我正在尝试使用Mousetrap库从 React 中的另一个组件调用一个函数。
class Parent extends React.Component {
constructor() {
super();
}
...
render() {
return(...);
}
}
class Child extends React.Component {
constructor() {
super();
this.state = { show: false };
this.open = this.open.bind(this);
this.close = this.close.bind(this);
}
open() {
this.setState({ show: true });
}
close() {
this.setState({ show: true });
}
render() {
return(...);
}
}
现在,我想做的是Mousetrap.bind('e', function(e) { Child.open() });
在父级的某个地方调用(因为父级将被渲染,而子级只会在此命令上渲染)。但是,我不确定在哪里实际包含它。在构造函数中调用它会更好,或者在某个地方render()
,或者我没有考虑过的其他地方?