17-react-best-pratices-2024

  1. 17 個必要的練習
  2. 總結:

參考來源 ByteGrad

17 個必要的練習

  1. Constants / Hard-coded values / Magic values
  2. Folder structure
  3. Components: when to create them
  4. Avoid unnecessary markup (div’s)
  5. IMPORTANT: don’t add layout styles to reusable component
  6. Use TypeScript (why & where in React)
  7. IMPORTANT: Keep components simple (children pattern, updater function for useState)
  8. IMPORTANT: Don’t use setter function from useState, instead create a separate event handler function
  9. IMPORTANT: Naming props (when prop is a function)
  10. useMemo, useCallback & React.memo() for performance
  11. When to use updater function from useState
  12. Use single state instead of multiple states
  13. IMPORTANT: one source of truth in programming. Keep track of a ‘selected’ or ‘active’ item by its id., NOT its whole object!
  14. IMPORTANT: use the URL for some state (filters, pagination, etc.), not useState
  15. Keep useEffect simple: one concern per useEffect
  16. IMPORTANT: instead of fetching data in useEffect, consider alternatives
  17. IMPORTANT: improve overall structure with components, custom hooks and utility functions

總結:

  1. 不要害怕創建 Components,就算是 not reuseable,因為 Components 使你的程式碼更乾淨。

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以邮件至 kimfei2014@gmail.com
github