问答详情
源自:3-6 长久保存数据——useRef

为什么ref.current会重新触发渲染?

useEffect(() => {

ref.current = setInterval(() => {

setNum(num => num + 1)

}, 1000);

console.log(ref.current);

ref.current = 111;

console.log(ref.current);

setTimeout(() => {

ref.current = 2222

}, 5000)

}, []);

useEffect(() => {

console.log(num);

if (num > 10) {

console.log('num大于10', ref.current);

clearInterval(ref.current);

}

}, [num]);


提问者:慕函数8463949 2022-05-01 15:43

个回答