我有一个关于如何在一个组件中触发不同组件的减速器值更改的函数的问题。
我目前在我的Home组件中有一个运行在ComponentDidMount. 如果我的值为this.props.reducer.runtrue,则函数会触发。初始状态设置为,true因此该函数在应用程序首次加载时触发。
在我的Profile组件中,我有一个拨动开关,可以切换this.props.reducer.run到true或false。Home当 reducer 值发生变化时,是否可以触发我的函数?
下面是我拥有的代码的模型,我想知道ComponentDidUpdate我是否应该看一下,如果是的话,一些指导将不胜感激:
主页.js
...
myFunction() = > {
console.log('fired')
}
ComponentDidMount() {
//runs on load
if(this.props.reducer.run == true) {
this.myFunction()
}
}
...
**Profile.js**
...
toggleRun = () => {
this.props.setRun(!this.props.reducer.run)
}
...