React Context or Redux? What's difference about rerendering (performance)?
Tags: react, javascript, redux, programming
Published: 2021-12-03 13:44:11

## Table of Contents
1. [🎯 Purpose](#purpose)
2. [🧸 Demo app](#demo)
3. [🗽 How to share a state](#share-state)
  - [Share a state by Props](#props)
     - [Illustration](#props-illustration)
  - [Share a state by React Context](#react-context)
     - [Illustration](#react-context-illustration)
  - [Share a state by Redux](#redux)
     - [Illustration](#redux-illustration)
  - [Share a state with other tools like Mobx, Recoil etc](#mobx-recoil)
4. [🎦 Rendering demonstration vidéo - Props vs React Context vs Redux](#video)
5. [❓So?](#so)
6. [🧑‍🚀 Further](#further)
7. [📖 Some Reading](#some-reading)

---

> The purpose is to develop some ideas in order to provoke a thinking for readers and for myself.
Happy reading.

---

## 🎯 Purpose <a name="purpose"></a>

What's the difference between React Context and Redux according to performance?

The question "Which one is the best to use for a better productivity/code?" is an other question that we don't looking for an answer here.

## 🧸 Demo app <a name="demo"></a>

To illustrate this article, I created a github project that you can clone and execute on your own computer. 

You can check it out if you want but it's not necessary to understand this article.

[https://github.com/AnthoGdn/labo_redux_vs_react-context](https://github.com/AnthoGdn/labo_redux_vs_react-context)

## 🗽 How to share a state <a name="share-state"></a>

With React, we have some possibilities to share a state into all components of the application. We can use Props, React Context, Redux, Mobx, Recoil etc...

You can find some code examples on my github project. (shared above)

### Share a state by Props <a name="props"></a>

The idea is to create a state into a component (we name it " common ancestor" here) and share it to its children. We need to share "setter functions" too.

```tsx
const [text, setText] = useState('')
// setText is a setter.
```

Every times we use a setter to change state, it's going to change the state into **all** common ancestor component, so it's going to rerender (paints again view) ancestor component and all its children.

<aside>
⚠️ It could be bad for performance if the page contains heavy graphic elements and graphic computations.
</aside>

#### Illustration <a name="props-illustration"></a>

![Share state by props](https://raw.githubusercontent.com/AnthoGdn/labo_redux_vs_react-context/master/doc/shared-with-props.drawio.png)

### Share a state by React Context <a name="react-context"></a>

React Context is exactly the same that Props. It's just syntactic sugar. 

Every times we modify state into common ancestor component, it's rerender common ancestor component and all its children.

<aside>
⚠️ It could be bad for performance if the page contains heavy graphic elements and graphic computations.
</aside>

#### Illustration<a name="react-context-illustration"></a>

![Share state by React Context](https://raw.githubusercontent.com/AnthoGdn/labo_redux_vs_react-context/master/doc/shared-with-context.drawio.png)


### Share a state by Redux <a name="redux"></a>

We branch a state to an ancestor component. React will not rerender common ancestor. It will rerender only component which use the part of the state that change.

<aside>
⚠️ The behaviour is not the same that React Context.
</aside>

#### Illustrations <a name="redux-illustration"></a>

![Share state by Redux](https://raw.githubusercontent.com/AnthoGdn/labo_redux_vs_react-context/master/doc/shared-with-redux.drawio.png)


### Share a state with other tools like Mobx, Recoil etc <a name="mobx-recoil"></a>

<aside>
I've never used these tools. But I think it's the same rendering behaviour than Redux. 🤷 
</aside>

[https://recoiljs.org](https://recoiljs.org)
[https://mobx.js.org](https://mobx.js.org)


## 🎦 Rendering demonstration vidéo - Props vs React Context vs Redux <a name="video"></a>

{% vimeo 652639626 %}

We can see the result of the 3 rerendering when we change the state with props, React Context and Redux.
You can see that with Redux, component 1 and 3 are not rerendering when we change state.


## ❓So? <a name="so"></a>

People asks me sometimes witch one is better to use. What's the best?

I think there is no best. It depends of the context of your application.

Now, you know what's the behaviour of these tools on the rendering of your components.

According of the context of your application, it's possible that one of these tools be better than the other. 

- If your state will not change often in the user navigation, so you can use what you want (rendering thinking).
- If your state will change often, it could be better to use Redux, specially if your "ancestor component" contains heavy graphic elements. For example, it's probably not a good idea to use React Context in a messaging or websocket or Server-Sent Events, because state could change a lot in short time. 🤷

Don't forget you can use React Context and Redux in the same application. Some people can say it's not recommended to use both in same application but why not? Using Redux for specific business logic of your product and using React Context for an other. Why not because React Context is included in React and it's just syntactic sugar of props. It costs no more to use it if you want to. 🤷

**Tips**: It's probably not a big deal sometimes to rerender components for nothing with React Context. The most important that users don't feel it. Don't over engineer is you don't need to.

**Again**:
The question "Which one is the best to use for a better productivity/code?" is another question that we don't looking for an answer here.

## 🧑‍🚀 Further <a name="further"></a>

- This point of view is theoretical. I didn't measure performance and demonstrate performance problem with React Context. It could be a purpose for a next article why not. 🤷

- Also, it's possible to use [React Memo](https://reactjs.org/docs/hooks-reference.html#usememo) with React Context to avoid useless rerendering but it could be exhausting to use than Redux don't you think? 🤷

- There is no need anymore to use global state to store http response data. For this, there exists library to fetch, cache and update data. [React Query](https://react-query.tanstack.com/) is great for that. Or [Apollo client](https://www.apollographql.com/docs/react/) for Graphql http requests.

## 📖 Some Reading <a name="some-reading"></a>

[Context - React](https://reactjs.org/docs/context.html)

[Redux - A predictable state container for JavaScript apps. | Redux](https://redux.js.org/)

[How to destroy your app performance using React contexts](https://thoughtspile.github.io/2021/10/04/react-context-dangers/)

---
Written by Anthony Godin, [LinkedIn](https://www.linkedin.com/in/anthony-godin-).