Let’s go over the most common mistakes you might be making in your React code right now, plus how to fix them.
If you want to create amazing React applications, it’s essential to avoid many common errors along the way.
In this article, we’ll not only cover how to fix your mistakes quickly, but also give you some awesome design patterns to make your code better and more reliable going forward.
1. Don’t pass state variables to setState in React
In the code below, we have a todo application that displays an array of todos (in
We can add new todos in the
AddTodo component, which updates the
todos array in App.
What’s the problem with the props that we have passed to
We are adding the new todo to the
todos array and then setting state as we should. This will update the displayed todos in the
However, since the new state is based off of the previous state, we do not need to pass down the todos array.
Instead, we can access the previous todos state by writing a function within the setState function. Whatever we return from this function will be set as the new state.
In other words, we only need to pass down the
setTodos function to properly update state:
2. Make your React components single responsibility
In the application below, we’re fetching a number of the users from an API within our app component, putting that user data in a state, and then displaying it within our user interface.
What is the problem with the
In our component, we’re doing multiple things.
We are not only doing remote data fetching from a server, but we are also managing state as well as displaying that state with JSX.
We are making our component do multiple things. Instead, your components should do only one thing and do that thing well.
This is one key design principle from the acronym SOLID, which lays out five rules for writing more reliable software.
The S in SOLID stands for the “single-responsibility principle”, an essential one to use when writing React components.
We can divide our
App component into separate components and hooks that each have their own responsibility. First, we will be extracting the remote data fetching to a custom React hook.
This hook, which we’ll call useUserData, will take care of fetching the data and putting it in local state.
After that, we will call the hook within
App to access our
However, instead of displaying user data directly within our return statement in
App, we will create a separate
User component which will contain all of the JSX necessary to display each element in that array, plus any related styles (if we have any).
After this refactoring, our components now have a clear, individual task to perform, which makes our app much easier to understand and extend.
3. Make your side effects single responsibility
App component below, we are fetching both user and post data.
When the location – the URL – of our app changes, we fetch both the user and post data.
We display a new post if the URL changes, but do we need to fetch that every time the location changes?
In much of your React code, you may be tempted to stuff all of your side effects within a single use effect function. But doing so violates the single responsibility principle we just mentioned.
This can result in problems such as performing side effects when we don’t need to. Remember to keep your side effects to a single responsibility as well.
In order to fix our code, all we need to do is call
getAuthUser within a separate use effect hook. This ensures it is not called whenever the location pathname changes, but only once when our app component mounts.
4. Use ternaries instead of
&& in JSX
Let’s say we are displaying a list of posts in a dedicated component,
It makes sense to check whether we have posts before we iterate over them.
posts list is an array, we can use the
.length property to check and see if it is a truthy value (greater than 0). If so, we can map over that array with our JSX.
We can express all this with the and
However, you might be surprised with what we see, if we were to execute such code. If our array is empty, we don’t see nothing – we see the number 0!
What? Why is this?!
&& operator doesn’t look at the right hand side of the expression. It just returns the left hand side – 0.
What is the best way to fix this and to prevent such errors in the future?
In many cases we shouldn’t use the and operator, but instead use a ternary to explicitly define what will be displayed in the event that condition is not met.
If we were to write the following code with a ternary, we would include the value
null in the else condition to ensure that nothing is displayed.
By using ternaries instead of
&&, you can avoid many annoying bugs like this one.
Thanks for reading!
The #1 Way to Learn React in Record Time
The React Bootcamp takes everything you should know about learning React and bundles it into one comprehensive package, including videos, cheatsheets, plus special bonuses.
Gain the insider information 100s of developers have already used to master React, find their dream jobs, and take control of their future:
Click here to be notified when it opens