Getting a custom Gutenberg component’s state from outside that component

I’ve registered a sidebar component in the block editor. I’d like to access the state of the component (MyPlugin) from outside of that component, in some arbitrary JavaScript in the same file. Is it possible to access the state via e.g. the wp object?

Or, is it possible, and would it be better to do this using props?

class MyPlugin extends Component {
constructor() {
super( …arguments );

// initial state
this.state = {
key: ‘my_key’,
value: ”
}
}

render() {
return el(
PluginPostStatusInfo,
{
className: ‘my-panel-class’
},
el(
TextControl,
{
name: ‘my_text_control_name’,
label: __( ‘My label’, ‘my-text-domain’ ),
value: this.state.value,
onChange: ( value ) => {
this.setState( {
value
})
}
}
)
);
}
}

registerPlugin( ‘my-plugin’, {
render: MyPlugin
} );

const myFunction = () => {
// this function is hooked into an action elsewhere,
// and needs to access the state of MyPlugin
}

Read more here:: Getting a custom Gutenberg component’s state from outside that component

Leave a Reply

Your email address will not be published. Required fields are marked *