【发布时间】:2019-12-12 19:30:59
【问题描述】:
在组件类中,我们使用componentDidMount、componentDidUpdate生命周期方法来更新状态。 例如)
componentDidMount() {
document.title = `You clicked ${this.state.count} times`;
}
componentDidUpdate() {
document.title = `You clicked ${this.state.count} times`;
}
它在每次渲染(componentDidUpdate)之后运行,包括第一次渲染(componentDidMount)。 在 useEffect 钩子中,我们可以像这样实现这个功能
useEffect(() => {
document.title = `You clicked ${count} times`;
});
这两种方法效果一样吗?
我阅读了 Reactjs.org 的这一部分,并在 React.js vs 16 上进行了尝试。 我认为这两种方法的效果是一样的。
useEffect(() => {
document.title = `You clicked ${count} times`;
});
【问题讨论】:
-
我读过 Dan Abramov 的一篇文章,它很好地解释了 useEffect 和那些生命周期之间的区别!这是链接:overreacted.io/a-complete-guide-to-useeffect
标签: reactjs react-hooks