React Best Practices You Need to Follow

Daniyal Samim
5 min readNov 23, 2021

While working on your React applications, you should be careful to follow certain good practices. This will help to keep your code very organized. In this article, I’ll be sharing certain best practices that every React developer should follow.

As you know, React is a library created by Facebook and it allows for integration with many interesting components. In fact, any developer can develop their own components and make them accessible to the community.

Today, I take the topic head-on and show you the most fundamental of the React best practices:

1. Keep your components compact

As we all know, with React, it’s possible to have huge components that execute a number of tasks. But a better way to design components is to keep them small so that one component corresponds to one function. Ideally, a single component should render a specific bit of your page or modify a particular behavior. There are many advantages to this:

  • Function-specific components can be standalone, which makes testing and maintenance easier.
  • Each small component can be reused across multiple projects.
  • Components executing general functions can be made available to the community.
  • With smaller components, it’s easier to implement performance optimizations.
  • It’s easier to update smaller components.
  • Bigger components have to perform harder and may be difficult to maintain.

The balance between creating one concise component and creating multiple function-specific components can vary from organization to organization. After all, you can have as many components as you want, and recombine them in any way you want to achieve the same end result.

2. Name your components wisely

It is always a good practice to name your components based on what the component does as it is easier to read and understand. Choose a name in such a way that fellow developers can easily recognize what the component does just by reading the name of the component. Make sure you always capitalize your components, else it would be considered as a normal HTML element.

3. Don’t Repeat Yourself (DRY)

A common rule for all code is to keep it as brief and concise as possible.

It’s no different here too since React best practices also instruct you to keep code brief and precise. One way to do this is to avoid duplication — Don’t Repeat Yourself (DRY).

You can achieve this by scrutinizing the code for patterns and similarities. If you find any, it’s possible you’re repeating code and there’s scope to eliminate duplication. Most likely, a bit of rewriting can make it more concise.

In React, you can make common components, so that they can be reused anywhere in your project. One of the ways to achieve the DRY principle across components is to use the Higher Order Component (HOC) concept.

4. Put CSS in JavaScript

Whenever you start a project, we usually write CSS in a separate .css or .scss file. But, when the project grows, this approach isn’t feasible. So, it’s always better to use libraries that help you write CSS in JS. Some of the popular libraries are:

5. Comment only where necessary

Attach comments to code only where necessary. This is not only in keeping with React best practices, but it also serves two purposes at the same time:

  • It’ll keep code visually clutter-free.
  • You’ll avoid a potential conflict between comment and code if you happen to alter the code at some later point in time.

6. Separate rendering logic and state logic

Components in React can be stateful or stateless. Stateful components store information about the component’s state and provide the necessary context. In contrast, stateless components have no memory and cannot give context to other parts of the UI. They only receive props (inputs) from the parent component and return you JSX elements. They are scalable and reusable and similar to pure functions in JavaScript.

One of React best practices is to keep your stateful data-loading logic separate from your rendering stateless logic. It’s better to have one stateful component to load data and another stateless component to display that data. This reduces the complexity of the components.

The later React versions v16.8 have a new feature — React Hooks, which write stateful function-related components. This may eventually eliminate the need for class-based components.

7. Use PropTypes (or TypeScript) for type checking

PropTypes is a form of type checking used in React. Without PropTypes or some form of type checking, we run into the risk of passing in the wrong data type to a component, which could cause some unexpected behavior in your application. Thus, by using PropTypes or TypeScript you can avoid unexpected glitches in your application.

8. Use snippet libraries

Code snippets help you to keep up with the best and most recent syntax. They also help to keep your code relatively bug free, so this is one of the React best practices that you should not miss out on.

There are many snippet libraries that you can use, like, ES7 React, Redux, JS Snippets, etc.

9. Use linting tools

ESLint is one of the popular linting tools that can be used in your React app. It will be needed to give a consistent code pattern and reduce syntax errors. You can also add Git hooks so that the code doesn’t get committed unless the code is error-free. This is very useful when you are working on a project along with a few other members.

10. Write test cases

Last but not the least, test your code. Once you follow all the other best practices in React, your code should be easy to test. There are different types of testing that you can do in React. Some popular ones are:

Unit Testing

Unit testing is the most basic form of testing. As the name suggests, it lets you test the smallest units in your code. Jest is the most popular testing framework for writing unit tests.

Component Testing

React components are individual reusable units that can be tested efficiently. This form of testing is very crucial in React, as we can test interactions with DOM.

End to End Testing

This basically means testing the application as a whole. It simulates how a user will click through the application and test it in a browser. The most popular and easy-to-use framework for end-to-end testing JavaScript (or anything that runs on a browser) is Cypress.

I hope these best practices are going to help you avoid any potential problems while building your application and you benefit from these tips that I discussed in this post. Thank you for reading!

If you have any React-related questions or projects, feel free to reach me on my LinkedIn account & email address.

--

--

Daniyal Samim

I’m a professional and passionate MERN Stack Developer focused on efficiency and constant learning.