ProNextJS

    Now it’s your turn! Try solving this exercise.

    Run locally (recommended)

    Code

    Start by cloning the project repository and follow instructions in the README.

    Or in browser

    Open on Gitpod
    Continue
    Loading...

    Transcript

    Jotai is a state management library that is built on the Atomic model. I think it's fair to say that the Atomic model is a little bit exotic in terms of the normal state management models that we've seen for the past decade as we've been working with React, and the fact that React didn't have good state management built-in,

    has created a flourishing of state managers, and so the Atomic model has come along recently as a new wave of state managers, including Recoil, which is from Meta, and Jotai from our friend Daishi Kato. Now, what makes Jotai unique is all of

    the plugins and all of the extra stuff that you can add onto Jotai. That's how it distinguishes itself from Recoil. Now, if you're like, what is Atomic model in the first place? Well, Atomic model is an interesting way of managing state. So every piece of state is an atom,

    and then you can mutate atoms directly. So we would have a cart atom, we would have a reviews atom, and then for example, if we wanted to have an average rating reviews atom, that could depend on the reviews atom and calculate that value.

    So it's really neat. I liken it to cells in a spreadsheet, and so when you've got cells and those cells in the spreadsheet reference each other in equations, then when you make a change, everything updates. That's how the Atomic model works, and that's really nice.

    So no more dependency arrays or any of that. It automatically calculates all those dependencies for you. Now, initially at Jotai, you would define atoms globally, just as global variables, and you would set and get them globally. Now, you can actually create a store and provide it using a store provider,

    and those stores can have instances of those particular atoms. For example, the cart atom and the reviews atom, and they exist within that store, and you can have multiple stores throughout the hierarchy, or you have one store top of the hierarchy like we're going to do in this one. With the layout, we're going to put the store provider

    there and then our atoms all the way down the tree. Again, this is the single store approach. So you're going to make sure that as you initialize those atoms, you're doing it the right way when it comes to reviews. So as you click along between the various product routes, you set those atoms correctly.

    So the objective of this exercise is to remove the existing React cart context and replace it completely with Jotai for the cart as well as the reviews. If you have any questions, be sure to look in the resources section. Good luck.