React 16.x & React Hooks API All In One
React 16.x & React Hooks API All In One
React Hooks API
https://reactjs.org/docs/hooks-intro.html
https://reactjs.org/docs/hooks-overview.html
React 16.x
React 16.8.0
https://reactjs.org/blog/2018/11/27/react-16-roadmap.html
https://legacy.reactjs.org/blog/2019/02/06/react-v16.8.0.html
https://discuss.reactjs.org/
https://dev.to/t/react
https://reactjs.org/docs/hooks-intro.html
https://reactjs.org/community/support.html
React Hook 解决了什么问题
Hooks are a new addition in React 16.8
They let you use state and other React features without writing a class.
we’ll continue by explaining why we’re adding Hooks to React and how they can help you write great applications
React 16.8.0 is the first release to support Hooks.
React Native supports Hooks since the 0.59 release of React Native.
90% Cleaner React With Hooks
Hooks provide a more direct API to the React concepts you already know: props, state, context, refs, and lifecycle.
问题
钩子解决了React在过去五年来编写和维护的成千上万个组件中遇到的各种看似无关的问题
Q:1 It’s hard to reuse stateful logic between components
wrapper hell, providers, consumers, higher-order components, render props
https://reactjs.org/docs/higher-order-components.html
https://reactjs.org/docs/render-props.html
A:1 Hooks allow you to reuse stateful logic without changing your component hierarchy
https://reactjs.org/docs/hooks-custom.html
Q:2 Complex components become hard to understand
the same componentDidMount method might also contain some unrelated logic that sets up event listeners, with cleanup performed in componentWillUnmount
Mutually related code that changes together gets split apart, but completely unrelated code ends up combined in a single method.
A: 2 Hooks let you split one component into smaller functions based on what pieces are related (such as setting up a subscription or fetching data)
https://reactjs.org/docs/hooks-effect.html#tip-use-multiple-effects-to-separate-concerns
Q:3 Classes confuse both people and machines
this, You have to remember to bind the event handlers.
https://babeljs.io/docs/en/babel-plugin-transform-class-properties/
For example, classes don’t minify very well, and they make hot reloading flaky and unreliable.
A:3 Hooks let you use more of React’s features without classes
https://reactjs.org/docs/hooks-overview.html
复盘React Hook的创造过程
react hooks 实现过程分析
https://github.com/shanggqm/blog/issues/4
React Hooks 调用顺序
https://overreacted.io/zh-hans/why-do-hooks-rely-on-call-order/
性能优化
React Component Lifecycle
https://reactjs.org/docs/react-component.html
https://reactjs.org/docs/state-and-lifecycle.html
react-lifecycle-methods-diagram
http://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/
16.3
16.4
demos
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/10101124.html
未经授权禁止转载,违者必究!