我已经在块编辑器中注册了一个侧栏组件。我想进入state
组件的(MyPlugin
) 从该组件外部,在同一文件中的任意JavaScript中。是否可以通过访问状态,例如wp
对象
或者,是否有可能,使用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
}
我最终想做的是从一个单独的函数访问textbox的值。我不介意如何实现这一点,例如state
或props
或者其他方式。我想另一种方法是让组件在其更改时将其值写入全局变量,但这似乎有点笨拙。