Simplifying React State Management with Jotai

Shehzad Ahmed
3 min readApr 28, 2023

--

Jotai is a state management library for React that simplifies the management of application state. It provides a clean, flexible, and predictable way to handle state in your React applications. This article will explore how to use Jotai in React to manage your application state.

Getting Started

To get started with Jotai, you must have React installed in your project. You can use Jotai in your project by installing it from NPM using the following command:

npm install jotai

Alternatively, you can install it using Yarn:

yarn add jotai

Once you have installed Jotai, you can import it into your React application using the following code:

import { createAtom, useAtom } from 'jotai';

Creating an Atom

Atoms are the fundamental building blocks of Jotai. An atom is a piece of state that can be read and written to. You can create an atom using the createAtom function. Here is an example:

const countAtom = createAtom(0);

The createAtom function creates a new atom with an initial value of 0. You can now use the countAtom atom to manage the state of your application.

Using an Atom

To use an atom in your React component, you can use the useAtom hook. The useAtom hook takes an atom as an argument and returns an array with two values: the current value of the atom and a function to update the value of the atom. Here is an example:

function Counter() {
const [count, setCount] = useAtom(countAtom);
}

function increment() {
setCount((count) => count + 1);
}

function decrement() {
setCount((count) => count - 1);
}

return (
<div>
<h1>{count}</h1>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
</div>
);
}

In this example, we are using the countAtom atom to manage the state of the Counter component. The useAtom hook is used to get the current value of the atom and the setCount function is used to update the value of the atom.

Updating an Atom

To update the value of an atom, you can use the set function. The set the function takes the new value of the atom as an argument. Here is an example:

function increment() {
countAtom.set((count) => count + 1);
}

function decrement() {
countAtom.set((count) => count - 1);
}

In this example, we are using the set function to update the value of the countAtom atom.

Conclusion

Jotai is a simple and flexible state management library for React. It provides an easy way to manage the state of your application by using atoms and hooks. In this article, we have explored how to use Jotai to manage your application state. We have seen how to create an atom, use an atom in a component, and update an atom. With Jotai, you can easily manage the state of your React application without any extra complexity.

Checkout full documentation here: https://jotai.org/docs/introduction

Find me on your favorite platform

  • Github — Follow me on GitHub for further useful code snippets and open source repos.
  • LinkedIn Profile — Connect with me on LinkedIn for further discussions and updates.
  • Twitter (X) — Connect with me on Twitter (X) for useless tech tweets.

--

--

No responses yet