[英]Call function from another component (React)
(React)我在一个组件中有一个函数(类叫做SideLogo)
toggleStatus(flag = !this.state.turned) {
this.setState({turned: flag})
}
我在组件中有另一个功能
_enterHandler() {
SideLogo.toggleStatus()
}
我正在尝试调用toggleStatus()
正如SakoBu已经提到的,你可以使用props公开一个函数。 看看这个小提琴: https : //jsfiddle.net/wzhdampx/
在您的其他组件中:
_enterHandler() {
this.toggleStatus();
}
render() {
return <SideLogo toggleStatus={func => this.toggleStatus = func} />;
}
在SideLog组件中:
componentDidMount() {
const { toggleStatus } = this.props;
if (toggleStatus) {
toggleStatus(this.toggleStatus);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.