DEV Community

Cover image for Recoil State Management in Nextjs
Next Dev
Next Dev

Posted on • Edited on

3 1

Recoil State Management in Nextjs

Welcome đź‘‹ guys, to this new tutorial blog post. In this blog post, I will teach you how to do state management in the nextjs with the help of recoil.

Atoms

In this tutorial, I will teach you how to use atoms from recoil in nextjs.

Think of atom-like a news report, whose work is to take news from one area to the whole nation.

đź« Situation

Let’s suppose, today you have to build a modal for your website. An example of a modal is shown 👇

But how will you make it?

We can use useState hooks for that, but useState will not be efficient for this purpose. So, you need to use Recoil in this situation.

Step1: Download Recoil

yarn add recoil

Step2: Go to the _app.js file and wrap components with RecoilRoot:


Step3: Make a atoms folder in the root directory. Then make a atoms.ts file in that folder.

Step4: Make atom for modalSwitch:


Step5: Use this atom in modal.tsx for making modal functional.



Step6: Use this atom in index.tsx for making modal functional.

Now you can try it

If any error comes, feel free to join the discord server:
https://discord.gg/hN4Wc5VR4M

Thanks for reading this blog post.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (1)

Collapse
 
sweethuman profile image
Gheorghe Avram •

This apparently doesn't work you have Client-Side only component that relies on Recoil.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

đź‘‹ Kindness is contagious

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

Okay