Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React 为什么要推出 Hooks?到底解决了什么问题? #101

Open
nmsn opened this issue Oct 18, 2023 · 1 comment
Open

React 为什么要推出 Hooks?到底解决了什么问题? #101

nmsn opened this issue Oct 18, 2023 · 1 comment
Labels

Comments

@nmsn
Copy link
Contributor

nmsn commented Oct 18, 2023

No description provided.

@nmsn nmsn added the React label Oct 18, 2023
@nmsn
Copy link
Contributor Author

nmsn commented Oct 18, 2023

Class 的缺点

  1. 组件复用逻辑困难
    • 高阶组件、renderProps 相对 hooks 来说更加难以理解且无法做到逻辑与组件拆分
    • 导致组件层级加深
  2. 复杂组件难以理解
    • 生命周期是固定的(一个组件一个生命周期只能声明一次),无法做到更新的划分
    • 可能会导致生命周期中塞入大量的逻辑(如 didMmount 中大量请求)
  3. Class 难用
    1. this 的使用更加复杂且容易出错(this 指向问题)
    2. Class 使用方式会使代码压缩更加困难,也会使热重载出现问题,跟 ESMdoule 不搭

Hooks 解决了哪些问题

  1. 组件的复用更加容易
  2. 组件中需要复用的逻辑可以单独抽离进行封装
  3. 函数式编程风格更加清爽,代码可读性增强

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant