Hooks
React Hooks
- 多个状态不会产生嵌套,写法还是平铺的
- 允许函数组件使用
state 和部分生命周期 - 更容易将组件的
UI 与状态分离
function useWindowWidth() {
const [width, setWidth] = React.useState(window.innerWidth);
useEffect(() => {
const onResize = () => {
setWidth(window.innerWidth);
};
window.addEventListener("resize", onResize);
return () => {
window.removeEventListener("resize", onResize);
};
}, [width]);
return width;
}
不过
Links
- https://reactjs.org/docs/hooks-faq.html
- https://reactjs.org/blog/2019/02/06/react-v16.8.0.html
- https://fettblog.eu/typescript-react/hooks/
- https://mp.weixin.qq.com/s/P5XZO5j494rGczXqKIza5w
- https://mp.weixin.qq.com/s/LrwPFDK2YCjcxAfw79N9Tg
- https://mp.weixin.qq.com/s/968ukIjEhhEOeLD5SQoKaw
- https://www.zhihu.com/question/338443007/answer/773530095
- https://blog.csdn.net/qq_41384351/article/details/90048454
- https://mp.weixin.qq.com/s/YEs5nH4aOAxOPYuW8oVlBA
- https://segmentfault.com/a/1190000020120456
- https://itnext.io/optimizing-react-code-with-hooks-3eaaf5978351