DEV Community

Dalcio
Dalcio

Posted on

3 1

Context API & State Managers

For some reason libraries like Redux are built using Context API. And when someone in the React world gets to know tools like Context API, Redux, Zustand, Recoil and so on, a lot of times they get stuck in the very common doubt (at least I've been through it), which is When should I use the Context API instead of a State Manager?. Well here's what I think about it.

Before answering, let's go through some definitions.

What's Context API?

The Context API represents a way of sharing variables or global states with its internal components (avoiding situations like Prop drilling).

What're State Managers?

They are tools that help us manage the most complex states of an application. Controlling how data can be mutated.

According to Open Replay, in the React ecosystem the most popular State Managers are: Redux* *, **Recoil, Zustand, Jotai, Rematch and the Hookstate.

And now?

OK, now that you know what Context API is and what State Managers are, let me just say that anything you can do with State Managers you can do with Context API, but the point is: Is it worth it? or Should I spend time on this?.

And I tell you no, it's not worth it, after all, why reinvent the wheel?

What going on, is that the State Managers already provide a well-elaborated tool to be able to manage our states as efficiently as possible and if we use the Context API instead of libs like Redux, we would have to worry about handling the states to avoid unnecessary rendering affecting the performance of our application.

When we should Context API?

  • Theming — Swipe down the app theme
  • i18n — Transmit translation messages
  • Authentication — Pass the current authenticated user
  • Atomic Component Globalization: When we have a component with some states that need to be used by more internal components.

Image description

References

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay