Mah*_*aji 3 javascript reactjs react-lifecycle react-hooks react-lifecycle-hooks
我在带有依赖项的功能组件内使用 useEffect 钩子,以便依赖项发生变化,useEffect 函数将像这样重新运行:
const [show, setShow] = React.useState(false);
React.useEffect(() => {
console.log("Do something")
} , [show]);
Run Code Online (Sandbox Code Playgroud)
我想知道 React 的类组件中有什么可以做到这一点?有没有任何生命周期方法可以实现此功能?
componentDidMount您可以使用和的组合componentDidUpdate:
componentDidMount(){ //use this method if you want to trigger the side effect first time
console.log("Do something")
}
componentDidUpdate(prevProps,prevState) {
if (this.state.show !== prevState.show) {
console.log("Do something");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4264 次 |
| 最近记录: |