17 個必要的練習
- Constants / Hard-coded values / Magic values
- Folder structure
- Components: when to create them
- Avoid unnecessary markup (div’s)
- IMPORTANT: don’t add layout styles to reusable component
- Use TypeScript (why & where in React)
- IMPORTANT: Keep components simple (children pattern, updater function for useState)
- IMPORTANT: Don’t use setter function from useState, instead create a separate event handler function
- IMPORTANT: Naming props (when prop is a function)
- useMemo, useCallback & React.memo() for performance
- When to use updater function from useState
- Use single state instead of multiple states
- IMPORTANT: one source of truth in programming. Keep track of a ‘selected’ or ‘active’ item by its id., NOT its whole object!
- IMPORTANT: use the URL for some state (filters, pagination, etc.), not useState
- Keep useEffect simple: one concern per useEffect
- IMPORTANT: instead of fetching data in useEffect, consider alternatives
- IMPORTANT: improve overall structure with components, custom hooks and utility functions
總結:
- 不要害怕創建 Components,就算是 not reuseable,因為 Components 使你的程式碼更乾淨。
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以邮件至 kimfei2014@gmail.com