DEV Community

Cover image for React State Management with Recoil
Kinga Hunyadi
Kinga Hunyadi

Posted on • Originally published at kinga.codes on

React State Management with Recoil

Recoil is a state management library for React. It is still in experimental phase, but it looks really promising. The best thing about Recoil is that it works and thinks like React. The most important concepts of Recoil are atoms and selectors.

Atoms are units of state, while selectors are pure functions that calculate derived data from state. Selectors accept both atoms and other selectors as input. Components can subscribe to selectors or atoms, and will be re-rendered when the selectors or atoms change.

recoil

Created with Sketchpad

I will explain how Recoil can manage your application’s state through some examples. No, it won’t be another todo app. Our app will show a list of songs, and for each song we can get some extra info. I will share the GitHub repository at the end of the article.

First of all, we need to create a new React app:

npx create-react-app recoil-example
cd recoil-example
yarn
yarn start
Enter fullscreen mode Exit fullscreen mode

Check that your app works on localhost:3000, you should see a page like this:

recoil

Then we need to add Recoil to our app:

yarn add recoil
Enter fullscreen mode Exit fullscreen mode

We need to wrap our components that use Recoil in RecoilRoot. We can replace the content of App.js with:

// App.js

import React from 'react';
import { RecoilRoot } from 'recoil';

import './App.css';

const App = () => (
  <div className={'App'}>
    <RecoilRoot>
      <h1>Recoil Example</h1>
    </RecoilRoot>
  </div>
);

export default App;
Enter fullscreen mode Exit fullscreen mode

Our app should still work and show the changes we made:

recoil

We will create a real-world-like example, so we will start with our client:

// client.js

const songList = [
  { id: 1, title: 'Bohemian Rhapsody' },
  { id: 2, title: 'Purple Rain' },
  { id: 3, title: 'One' },
  { id: 4, title: 'Eternal Flame' },
];

const songDetails = [
  { id: 1, artist: 'Queen', year: 1975 },
  { id: 2, artist: 'Prince', year: 1984 },
  { id: 3, artist: 'U2', year: 1992 },
  { id: 4, artist: 'The Bangles', year: 1989 },
];

export const getSongs = async () =>
  new Promise(resolve => setTimeout(() => resolve(songList), 500));

export const getSongById = async id =>
  new Promise(resolve => {
    const details = songDetails.find(s => s.id === id);
    return setTimeout(() => resolve(details), 500);
  });
Enter fullscreen mode Exit fullscreen mode

Now that we have our client functions, we can implement the atoms and selectors that will manage our app’s state. Each atom and selector will have a unique id. We will start with loading the song list. As our client function returns a promise, the selector’s get function will be async:

// selectors.js

import { selector } from 'recoil';
import { getSongs } from './client';

export const songsQuery = selector({
  key: 'songs',
  get: async () => {
    const response = await getSongs();
    return response;
  },
});
Enter fullscreen mode Exit fullscreen mode

Next step is to create a component which renders the list of songs. We need to connect our component to the selector we just created. Recoil has some useful hooks for this:

  • useRecoilState — returns the value of the given state and the setter function for updating the value of given the state;
  • useRecoilValue — returns the value of the given state;
  • useSetRecoilState — returns the setter function for updating the value of given the state.

We will create the Songs component:

// Songs.js

import React from 'react';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { songsQuery } from './selectors';
import { currentSongIDState } from './atoms';

const Songs = () => {
  const songs = useRecoilValue(songsQuery);
  const setCurrentSongID = useSetRecoilState(currentSongIDState);

  return (
    <>
      <h2>Songs</h2>
      {songs.map(song => (
        <div key={song.id}>
          <p onClick={() => setCurrentSongID(song.id)}>{song.title}</p>
        </div>
      ))}
    </>
  );
};

export default Songs;
Enter fullscreen mode Exit fullscreen mode

We should note that our selector is async, but React render functions are synchronous. Here comes in React Suspense, which handles pending data. We could also handle pending state with Recoil’s Loadable, or implement a handler from scratch, but we will use Suspense now:

// App.js

import React, { Suspense } from 'react';
import { RecoilRoot } from 'recoil';
import Songs from './Songs';

import './App.css';

const App = () => (
  <div className={'App'}>
    <RecoilRoot>
      <Suspense fallback={<span>Loading...</span>}>
        <Songs />
      </Suspense>
    </RecoilRoot>
  </div>
);

export default App;
Enter fullscreen mode Exit fullscreen mode

Now in our browser we should see the list of songs:

recoil

That was easy, right?

Now let’s see how can we get the details of a song. When we select a song, we want to see its details, like the artist and the year of release. We need to remember the current song ID. The ID is just a simple value, it will not be computed, so we will create an atom for this, instead of a selector:

// atoms.js

import { atom } from 'recoil';

export const currentSongIDState = atom({
  key: 'currentSongID',
  default: '',
});
Enter fullscreen mode Exit fullscreen mode

Based on the current song ID we want to get the song details. We need another selector which calls the client function with the current song ID. Selectors can read other atoms and selectors using the get argument of the get function. I know it sounds a little confusing, but the next example will make it more clear:


// selectors.js

import { selector } from 'recoil';
import { currentSongIDState } from './atoms';
import { getSongs, getSongById } from './client';

// ...

export const currentSongQuery = selector({
  key: 'currentSong',
  get: async ({ get }) => {
    const response = await getSongById(get(currentSongIDState));
    return response;
  },
});
Enter fullscreen mode Exit fullscreen mode

We will now create the CurrentSong component, which renders the details of the selected song:

// CurrentSong.js

import React from 'react';
import { useRecoilValue } from 'recoil';
import { currentSongQuery } from './selectors';

const CurrentSong = () => {
  const currentSong = useRecoilValue(currentSongQuery);

  return currentSong ? (
    <>
      <h2>Current Song Details:</h2>
      <p>Artist: {currentSong.artist}</p>
      <p>Released: {currentSong.year}</p>
    </>
  ) : null;
};

export default CurrentSong;
Enter fullscreen mode Exit fullscreen mode

Then we can add it to our Songs component. The currentSongIDState atom can be updated from the component by using the setter function returned by useRecoilState. (Note that I didn’t want to add it to the App component, because I don’t want to show the “Loading…” state when nothing is selected. Of course, we could structure our app better, but for now it’s just fine):

// Songs.js

import React, { Suspense } from 'react';
import { useRecoilValue, useRecoilState } from 'recoil';
import { songsQuery } from './selectors';
import { currentSongIDState } from './atoms';
import CurrentSong from './CurrentSong';

const Songs = () => {
  const songs = useRecoilValue(songsQuery);
  const [currentSongID, setCurrentSongID] = useRecoilState(currentSongIDState);

  /*
   * as an alternative, we could declare them separately:
   * const currentSongID = useRecoilValue(currentSongIDState);
   * const setCurrentSongID = useSetRecoilState(currentSongIDState);
   */

  return (
    <>
      <h2>Songs</h2>
      {songs.map(song => (
        <div key={song.id}>
          <p onClick={() => setCurrentSongID(song.id)}>
            {song.title} {song.id === currentSongID && '*'}
          </p>
        </div>
      ))}
      {currentSongID && (
        <Suspense fallback={<span>Loading...</span>}>
          <CurrentSong />
        </Suspense>
      )}
    </>
  );
};

export default Songs;
Enter fullscreen mode Exit fullscreen mode

If we click on a song we should see the details below the song list:

recoil

It was easy and fun so far, while working with read-only data, but in real-world apps we want our app’s state to get updated after doing an update on the server. For example, we might want to add new songs to our list. Here it becomes a little more complicated.

If you are used to work with other state management libraries, like Redux, then you know that the “global” state can be updated after updating the data on the server. Recoil does not have a “global” state, like other state management libraries, but coupled to RecoilRoot. That means the state can not be updated outside of the components/hooks.

But there is still hope... with Recoil we can achieve this by subscribing to server updates from useEffect, and updating the state from there. I know this is not ideal, but this API is still under development, and Recoil might handle this out-of-the-box.

In conclusion, comparing it with other state management libraries (like Redux), it seems more “React-like” and simpler and easier to learn, so it might be a good alternative in the future.

You can find the GitHub repository here. Thank you for reading this article.

Oldest comments (6)

Collapse
 
oliverradini profile image
OliverRadini

Looks promising, nice to see a state library with selectors built in, really find that it helps to keep only necessary state, and to define as much derived state as possible. Out if the Tar Pit is a great paper to read, for anyone looking to understand concepts behind state management.

Collapse
 
kinga profile image
Kinga Hunyadi

Thank you! I am curious to see how the community will adopt it. I will definitely try to use it when it will be more stable.

Collapse
 
havespacesuit profile image
Eric Sundquist

I've spent the last few weeks converting a large part of our project to use this. We went from a tangled mess to a fairly straightforward solution rather painlessly. Very impressed so far!

The ability to avoid prop-drilling, massive-layers of contexts, or large state mgmt libraries with loads of boilerplate (looking at you Redux) all in a clean React-like interface is so refreshing!

As noted, one drawback is the inability to force a selector to re-query the server. You can do it with a hack to use a dependency on a atom. When you want to re-query, set the atom to the next number. Not great, but it gives you an imperative way to control a hard-reload.

Collapse
 
kinga profile image
Kinga Hunyadi

Wow, good job! Yes, that’s what I like about it most, it is “React-like” and we can avoid some boilerplate code.
Interesting hack with the atom, I will keep that in mind ☺️.

Collapse
 
cliffordfajardo profile image
Clifford Fajardo • Edited

@kinga great intro/getting start with recoil!
Any opinions on react-query or other similar server state client libraries?

  • I know this is a small example starter example you've shared 😀, but I was just curious about your thoughts on storing server data (example API response data) in places in client state tools like react context, recoil VS server state libraries (react-query..) etc?

Interesting thread here on twitter (scroll up after the tweet loads to the question)

Collapse
 
kinga profile image
Kinga Hunyadi • Edited

Thank you! I mostly used Redux for state management, as it was the most popular 😄. I want to give Recoil a chance in the future. But I did not use other libraries for state management. I used context only for things that should be shared between all (most) components, e.g. current theme, user. But I will definitely check those out, thanks for the suggestion. ☺️