useState() and useEffect() and useContext(), useCallback in react

  • for useState(), see:

https://www.freecodecamp.org/news/usestate-vs-redux-state-management/

另外请记住,更新state是asynchronous的,意味着如果刚set了state就读取其值,这个值应该还是old的值,解决solution是用useEffect,参见:https://dev.to/shareef/react-usestate-hook-is-asynchronous-1hia

 

  • for useReducer(), reducer是比state更好的状态管理, see:

https://react.dev/learn/extracting-state-logic-into-a-reducer

 

  • for useEffect(), see: 

https://www.freecodecamp.org/news/react-useeffect-absolute-beginners/

简单说就是:当前component之行完毕后会执行useEffect定义的第一个参数的函数,当dependency参数改变的时候也会重新之行useEffect的第一个函数 

另一变种 userLayoutEffect(): https://react.dev/reference/react/useLayoutEffect#measuring-layout-before-the-browser-repaints-the-screen

 

  • useContext(), see:

https://react.dev/reference/react/useContext

https://react.dev/learn/passing-data-deeply-with-context

将state放在父级组建中,所有子组件都能读取和使用。

https://react.dev/learn/scaling-up-with-reducer-and-context   (Reducer和context一起)

 

  • useCallback:

https://react.dev/reference/react/useCallback  (介绍userCallback缓存function和memo 一起提高render性能)

 

 

其他的hook,可以读这个:https://ijaycent.hashnode.dev/simplify-your-react-programming-effortlessly-with-these-8-amazing-hooks

 

posted @ 2023-10-09 15:21  saaspeter  阅读(13)  评论(0编辑  收藏  举报