useEffect的使用

useEffect(() => {
	document.title = `you click ${count} times`
},[])
  • useEffect函数里面写内容,相当于在componentDidMount和componentDidUpdate生命周期函数中写内容
  • useEffect第二个参数写[],相当于componentDidMount生命周期函数
  • [count]:只监听count发生改变的时候,才会触发 componentDidUpdate生命周期函数
  • useEffect里面返回函数,在返回的函数中写内容相当于在componentDidMount与componentDidUpdate和componentWillUnmount生命周期函数中写内容
上一篇:React中useEffect的源码解读


下一篇:深入理解 React useLayoutEffect 和 useEffect 的执行时机