一看就是菜鸡,前端小丑真多,建议多看看 React Hooks 的官方文档.
https://reactjs.org/docs/hooks-intro.htmlhttps://reactjs.org/docs/hooks-faq.html#do-i-need-to-rewrite-all-my-class-componentsWe also understand that the bar for adding a new primitive to React is extremely high. For curious readers, we have prepared a detailed RFC that dives into motivation with more details, and provides extra perspective on the specific design decisions and related prior art.
Crucially, Hooks work side-by-side with existing code so you can adopt them gradually. There is no rush to migrate to Hooks. We recommend avoiding any “big rewrites”, especially for existing, complex class components. It takes a bit of a mind shift to start “thinking in Hooks”. In our experience, it’s best to practice using Hooks in new and non-critical components first, and ensure that everybody on your team feels comfortable with them. After you give Hooks a try, please feel free to send us feedback, positive or negative.