Common Mistakes React Developers Make – And How to Fix Them
Tags: javascript, react, codenewbie
Published: 2021-05-05 05:37:48

In this article, we'll see some of the common mistakes made by React developers and ways to avoid them.

So let's get started.

## Don't Forget that Every Route Change Mounts and Unmounts a Component

Whenever we're using routing in React application, we declare routes inside the `Switch` component so at a time only one component with the matching route is displayed.

Therefore, whenever we go from one route to another, the previously displayed component is unmounted and the component with the new matching route is mounted.

If you need to persist some data across route change, you need to declare it in the component which encapsulates the routes such as the `App` component in the following Code Sandbox or some other way of persisting data like using [local storage or session storage](https://javascript.plainenglish.io/everything-you-need-to-know-about-html5-local-storage-and-session-storage-479c63415c0a?source=friends_link&sk=f429aa5008683a3b0359db43f976efb3)

{% codesandbox hqz9x %}

As you can see in the above Code Sandbox, whenever we change the route by clicking on the links, we get the corresponding `console.log` displayed on the console to indicate that the previous component is unmounted and a new component is mounted.

## Don't Use the Wrong setState Syntax

Whenever we declare a state inside the class-based component, it's always an object like this:

```js
this.state = {
   counter: 0
}
```

so whenever we use the updater form of setState syntax to update the state, it looks like this:

```js
this.setState((prevState) => {
  return {
    counter: prevState.counter + 1
  };
});
```

As the state is an object, `prevState` is also an object so we access the `counter` using `prevState.counter`.

But when we're using functional component using React Hooks the state can be an object or a non-object value as shown below:

```js
const [counter, setCounter] = useState(0);
```

Here, the value of `counter` is not an object but it's a number so to update the state using updater syntax we write the code like this:

```js
setCounter((prevCounter) => prevCounter + 1);
```

Here, the `prevCounter` is a number so we don't use `prevCounter.counter` but just `prevCounter` or it can be simplified as shown below:

```js
setCounter((counter) => counter + 1);
```

> Check out my [this article](https://www.freecodecamp.org/news/what-is-state-in-react-explained-with-examples/) for complete introduction to React State

## Don't Call Hooks from Class Components

Starting with React version 16.8.0, React has introduced Hooks that allow us to write better React code
and make use of state and component life cycle methods inside functional components.

> Check out my [this article](https://levelup.gitconnected.com/an-introduction-to-react-hooks-50281fd961fe?source=friends_link&sk=89baff89ec8bc637e7c13b7554904e54) for an Introduction to React Hooks.

To make coding easy, there are many hooks provided by React like:

* `useParams` hook to access URL parameters when using React Routing
* `useHistory` hook to get access to history API inside any component
* `useRef` hook to get access to DOM element

and many other hooks.

But all of these hooks which usually starts with the `use` keyword work only inside the functional components. 

If you're having a class-based component then you can't use these hooks. You need to refactor your code to convert it to functional components otherwise, you might get an error as can be seen in the below screenshot:

![hook_error.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1620192585791/GDcHT4dfV.png)

## Don't Forget to Add a Key Prop When Using the Array map Method

Take a look at [this Code Sandbox Demo](https://codesandbox.io/s/quirky-shockley-bjd6z?file=/src/index.js).

Here, for displaying a list of items, we're using the following code:

```js
const Items = ({ items }) => (
  <ol>
    {items.map((item) => (
      <Item item={item} />
    ))}
  </ol>
);
```

In React, for displaying a list of items stored in an array, usually, the array `map` method is used.

But as soon as you add an item to the list in the above Code Sandbox, you will see a missing key warning displayed in the console.

![missing-key.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1620192605840/gTyypza_9.gif)

This is because every time you're using the array `map` method to loop over the items, you need to provide a unique `key` prop which React uses to identify which elements on the screen need to be re-rendered and adding `key` prop helps to avoids unnecessary re-rendering in your app.

Here's an updated [Code Sandbox Demo](https://codesandbox.io/s/boring-greider-olko7?file=/src/index.js) with the added `key` prop.

Here, we've provided a unique `key` prop to each element we're looping over like this:

```js
<Item item={item} key={index} />
``` 

Therefore, If you try adding some items, you will not get any warning in the console.

![added-key.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1620192629303/oH_ikIDU6.gif)

> Note: In the above code, as the elements not re-ordered or removed, using `index` as `key` works fine but If you're removing or changing the displayed elements order then you need to provide a unique key instead of using `index`.

## Don't Use Inline Functions the Wrong Way

Take a look at [this Code Sandbox Demo](https://codesandbox.io/s/stupefied-breeze-66nyr?file=/src/index.js).

Here, we've added some items to the state:

```js
const [items, setItems] = useState(["one", "two"]);
```

and we're looping over them to display on the screen:

```jsx
{items.map((item, index) => (
  <li key={index}>
    {item} <button onClick={handleRemoveItem(item)}>Remove</button>
  </li>
))}
```

If you check the application, you will see that no items are displayed on the screen and adding new items also does not work as can be seen below:

![wrong_inline.gif](https://cdn.hashnode.com/res/hashnode/image/upload/v1620192648610/g4ABKv5xH.gif)

This is because of the `onClick`  handler for the button:

```jsx
<button onClick={handleRemoveItem(item)}>Remove</button>
```

Here, we're calling the `handleRemoveItem` method when the user clicks on the button but the way we're calling the method is wrong.

So If we don't need to pass any parameter we use the following syntax:

```jsx
<button onClick={handleRemoveItem}>Remove</button>
```

But later If we decide to pass some parameter to the function, we need to call the handler inside inline function like this:

```jsx
<button onClick={() => handleRemoveItem(item)}>Remove</button>
```

Most React developers forget to add an inline function and then it takes hours of debugging to understand why something does not work.

Here's an updated working [Code Sandbox Demo](https://codesandbox.io/s/polished-moon-02iug?file=/src/index.js).

### Thanks for reading!

Starting with ES6, there are many useful additions to JavaScript like:

* ES6 Destructuring
* Import and Export Syntax
* Arrow functions
* Promises
* Async/await
* Optional chaining operator
and a lot more.

**You can learn everything about all the ES6+ features in detail in my [Mastering Modern JavaScript](https://modernjavascript.yogeshchavan.dev/) book.**

> Check out free preview contents of the book [here](https://www.freecodecamp.org/news/learn-modern-javascript/).

Also, you can check out my **free** [Introduction to React Router](https://yogeshchavan1.podia.com/react-router-introduction) course to learn React Router from scratch.

Want to stay up to date with regular content regarding JavaScript, React, Node.js? [Follow me on LinkedIn](https://www.linkedin.com/in/yogesh-chavan97/).

[<img src="https://gist.github.com/myogeshchavan97/98ae4f4ead57fde8d47fcf7641220b72/raw/c3e4265df4396d639a7938a83bffd570130483b1/banner.jpg">](https://bit.ly/3w0DGum)

[<img src="https://cdn.buymeacoffee.com/buttons/default-yellow.png" >](https://www.buymeacoffee.com/myogeshchavan97)