
Micro State Management with React Hooks
By :

So far, we haven't discussed how Jotai uses Context. In this section, we'll show how Jotai stores atom values and how atoms are reusable.
First, let's revisit a simple atom definition, countAtom
. atom
takes an initial value of 0
and returns an atom config, as follows:
const countAtom = atom(0);
Implementation-wise, countAtom
is an object holding some properties representing the atom behavior. In this case, countAtom
is a primitive atom, which is an atom with a value that can be updated with a value or an updating function. A primitive atom is designed to behave like useState
.
What is important is that atom configs such as countAtom
don't hold their values. We have a store
that holds atom values. A store
has a WeakMap
object whose key is an atom config object and whose value is an atom value.
When we use useAtom
, by default, it uses a default store
defined at the module level. However, Jotai provides...