React.useEffect(() => {
// 再此可以执行请求 定时器等操作
console.log('开启网络请求')
return () => {
// 返回值这个函数会在页面卸载的时候调用
// 可以取消定时器等操作
console.log('页面销毁')
}
}, [num]) // 指定要监听的变量,发生变化后会执行第一个参数(函数)
// 什么也不传的话,每一个useState变量变化都会执行
// 传入空数组[]的话,只会在页面挂载的时候执行
可以把useEffect看成下面三个生命周期的整合
componentDidMount
componentDidUpdate
componentWillUnmount