effect的更新依赖屈指可数
setCount(x => x + 1); // 传递参数为一个函数时候,默认传递的第一个参数是之前的值,这是useState的hook在处理 }, 1000); return () => clearInterval(id); }, []);
return <h1>{count}</h1>; }
// 使用useReducer function Counter({ step }) { const [count, dispatch] = useReducer(reducer, 0);
function reducer(state, action) { if (action.type === 'tick') { return state + step; } else { throw new Error(); } }
useEffect(() => { const id = setInterval(() => { dispatch({ type: 'tick' }); }, 1000); return () => clearInterval(id); }, [dispatch]);
return <h1>{count}</h1>; (编辑:ASP站长) 【免责声明】本站内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。 |
-
无相关信息