deno.land / x / jotai@v1.8.4 / docs / guides / atoms-in-atom.mdx

atoms-in-atom.mdx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
---title: Atoms in atomnav: 3.08---
`atom()` creates an atom config, which is an object, but it doesn't hold a value.Atom configs don't have string keys and we identify them with referential equality.In other words, we can use an atom config like a key.
## Storing an atom config in useState
First things first, we can store an atom config in useState.
```jsxconst Component = ({ atom1, atom2 }) => { const [selectedAtom, setSelectedAtom] = useState(atom1) const [value] = useAtom(selectedAtom) return ( <div> Selected value: {value} <button onClick={() => setSelectedAtom(atom1)}>Select an atom</button> <button onClick={() => setSelectedAtom(atom2)}> Select another atom </button> </div> )}```
Note that we can pass atoms configs as props.
It might not make any sense, but we could create an atom config on demand.
```jsxconst Component = () => { const [currentAtom, setCurrentAtom] = useState(() => atom(0)) const [count, setCount] = useAtom(currentAtom) return ( <div> Count: {count} <button onClick={() => setCount((c) => c + 1)}>+1</button> <button onClick={() => setCurrentAtom(atom(0))}>Create new</button> </div> )}```
## Storing an atom config in atom
Likewise, we can store an atom config as a value of another atom.
```jsxconst firstNameAtom = atom('Tanjiro')const lastNameAtom = atom('Kamado')
const showingNameAtom = atom(firstNameAtom)
const Component = () => { const [nameAtom, setNameAtom] = useAtom(showingNameAtom) const [name] = useAtom(nameAtom) return ( <div> Name: {name} <button onClick={() => setNameAtom(firstNameAtom)}> Show First Name </button> <button onClick={() => setNameAtom(lastNameAtom)}>Show Last Name</button> </div> )}```
It's possible to create a derived atom.
```jsconst derivedNameAtom = atom((get) => { const nameAtom = get(showingNameAtom) return get(nameAtom)})
// Or a shorter versionconst derivedNameAtom = atom((get) => get(get(showingNameAtom)))```
To avoid confusing what is in atoms, naming atoms explicitly would be important.Also, TypeScript type information would be helpful.
## Storing an array of atom configs in atom
Finally, the atoms in atom pattern is to store an array of atom config into an atom.
```jsxconst countsAtom = atom([atom(1), atom(2), atom(3)])
const Counter = ({ countAtom }) => { const [count, setCount] = useAtom(countAtom) return ( <div> {count} <button onClick={() => setCount((c) => c + 1)}>+1</button> </div> )}
const Parent = () => { const [counts, setCounts] = useAtom(countsAtom) const addNewCount = () => { const newAtom = atom(0) setCounts((prev) => [...prev, newAtom]) } return ( <div> {counts.map((countAtom) => ( <Counter countAtom={countAtom} key={countAtom} /> ))} <button onClick={addNewCount}>Add</button> </div> )}```
The benefit of this approach is, if you increment a count,only the corresponding Counter component re-renders and no other components re-render.
It is important to note that `anAtom.toString()` returns a unique id, which can be used as a `key` in a map.
### Hint for TypeScript users
```jsx<Counter countAtom={countAtom} key={`${countAtom}`} />```
## Storing a map of atom configs in atom
Likewise, we can store an object map instead of an array.
```jsxconst pricesAtom = atom({ apple: atom(15), orange: atom(12), pineapple: atom(25),})
const Fruit = ({ name, priceAtom }) => { const [price] = useAtom(priceAtom) return ( <div> {name}: {price} </div> )}
const Parent = () => { const [prices] = useAtom(pricesAtom) return ( <div> {Object.keys(prices).map((name) => ( <Fruit name={name} priceAtom={prices[name]} key={name} /> ))} </div> )}```
jotai

Version Info

Tagged at
a year ago