The provided content outlines 21 best practices for maintaining clean and maintainable React projects, emphasizing code quality and performance optimization.
Abstract
The article "21 Best Practices for a Clean React Project" offers practical advice for developers to enhance the quality and performance of their React applications. It covers a range of topics, from using JSX shorthand and ternary operators for cleaner conditional rendering to employing React fragments and memoization for improved performance. The author also discusses the importance of organizing CSS within JavaScript, using object destructuring, and avoiding unnecessary JS code within JSX. The article emphasizes the significance of naming conventions, import order, and the use of template literals for string building. Additionally, it provides guidance on prop naming, the use of quotes, and the proper application of self-closing tags and alt props in images. The overarching goal is to equip developers with the knowledge to write more readable, efficient, and maintainable React code.
Opinions
The author believes that React developers should take advantage of the language's features, such as JSX shorthand and ternary operators, to write more concise and readable code.
It is implied that organizing CSS within JavaScript is superior to traditional CSS methods, suggesting a preference for styled-components or similar libraries.
The use of React.PureComponent and Memo is strongly recommended for preventing unnecessary re-renders, indicating a performance-centric approach.
The article suggests that adhering to a specific import order (built-in, external, internal) improves code readability, reflecting a structured and methodical coding style.
The author advises against defining functions inside the render method, advocating for a clear separation of logic and presentation to enhance maintainability.
There is an emphasis on using camelCase for component instances and PascalCase for component definitions, highlighting the importance of React's naming conventions.
The recommendation to wrap multi-line JSX in parentheses suggests a preference for a more explicit and error-proof coding practice.
The article expresses a clear opinion against using underscores in method names, aligning with the broader JavaScript community's conventions.
The inclusion of an alt prop in <img> tags is presented as a non-negotiable best practice, underscoring the importance of accessibility in web development.
React is very unopinionated about how things should be structured. This is exactly why it’s our responsibility to keep our projects clean and maintainable.
Today, we will discuss some best practices to improve your React application’s health. These rules are widely accepted. As such, having this knowledge is imperative.
Everything will be shown with code, so buckle up!
1. Use JSX ShortHand
Try to use JSX shorthand for passing boolean variables. Let’s say you want to control the title visibility of a Navbar component.
Bad
Good
2. Use Ternary Operators
Let’s say you want to show a user’s details based on role.
Bad
Good
3. Take Advantage of Object Literals
Object literals can help make our code more readable. Let’s say you want to show three types of users based on their roles. You can’t use ternary because the number of options exceeds two.
Bad
Good
It looks much better now.
4. Use Fragments
Always use Fragment over Div. It keeps the code clean and is also beneficial for performance because one less node is created in the virtual DOM.
Bad
Good
5. Don't Define a Function Inside Render
Don’t define a function inside render. Try to keep the logic inside render to an absolute minimum.
Bad
Good
6. Use Memo
React.PureComponent and Memo can significantly improve the performance of your application. They help us to avoid unnecessary rendering.
Bad
Although the child component should render only once because the value of count has nothing to do with the ChildComponent . But, it renders each time you click on the button.
Good
Let’s edit the ChildrenComponent to this:
Now, no matter how many times you click on the button, it will render only when necessary.
7. Put CSS in JavaScript
Avoid raw JavaScript when writing React applications because organizing CSS is far harder than organizing JS.
Bad
Good
8. Use Object Destructuring
Use object destructuring to your advantage. Let’s say you need to show a user’s details.
Bad
Good
9. String Props Don’t Need Curly Braces
When passing string props to a children component.
Bad
Good
10. Remove JS Code From JSX
Move any JS code out of JSX if that doesn’t serve any purpose of rendering or UI functionality.
Bad
Good
11. Use Template Literals
Use template literals to build large strings. Avoid using string concatenation. It’s nice and clean.
Bad
Good
12. Import in Order
Always try to import things in a certain order. It improves code readability.
Bad
Good
The rule of thumb is to keep the import order like this:
Built-in
External
Internal
So the example above becomes:
13. Use Implicit return
Use the JavaScript feature implicit return in writing beautiful code. Let’s say your function does a simple calculation and returns the result.
Bad
Good
14. Component Naming
Always use PascalCase for components and camelCase for instances.
Bad
Good
15. Reserved Prop Naming
Don’t use DOM component prop names for passing props between components because others might not expect these names.
Bad
Good
16. Quotes
Use double quotes for JSX attributes and single quotes for all other JS.
Bad
Good
17. Prop Naming
Always use camelCase for prop names or PascalCase if the prop value is a React component.
Bad
Good
18. JSX in Parentheses
If your component spans more than one line, always wrap it in parentheses.
Bad
Good
19. Self-Closing Tags
If your component doesn’t have any children, then use self-closing tags. It improves readability.
Bad
Good
20. Underscore in Method Name
Do not use underscores in any internal React method.
Bad
Good
21. Alt Prop
Always include an alt prop in your <img > tags. And don’t use picture or image in your alt property because the screenreaders already announce img elements as images. No need to include that.
Bad
Good
Conclusion
There you go. Congratulations if you’ve made it this far! I hope you learned a thing or two from this article.
I hope you have a wonderful day! :D
Have something to say?
Get in touch withme via LinkedIn ormy Personal Website.