Skip to content

Commit ba914f8

Browse files
docs: update state management docs
1 parent dd16ec4 commit ba914f8

1 file changed

Lines changed: 110 additions & 4 deletions

File tree

Lines changed: 110 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,124 @@
11
---
22
id: jotai
33
slug: /jotai
4-
title: Jotai - state management
5-
sidebar_position: 2
4+
title: State management - jotai
5+
sidebar_position: 3
66
tags:
77
- Jotai
88
- State management
99
- React
1010
- React Native
11-
description: Doppler - automatically manage environment variables
11+
description: State management - jotai -
1212
---
1313

14-
## Jotai
14+
# State management - Jotai
15+
16+
## Description
1517

1618
This starter comes with jotai state management tool. Please check documentation on how it work in details:
1719

1820
- https://jotai.org/docs/introduction
21+
22+
## Examples
23+
24+
### Create atom
25+
26+
```tsx
27+
import { atom } from 'jotai'
28+
29+
export const isSignedInAtom = atom<boolean | null>(null)
30+
31+
export const userAtom = atom<User | null>(null)
32+
export const userNameAtom = atom<string | null>((get) => {
33+
const user = get(userAtom)
34+
const userName = user.userName
35+
36+
return userName
37+
})
38+
```
39+
40+
### Get atom value
41+
42+
Get with hook
43+
44+
```tsx
45+
import { useAtomValue } from 'jotai'
46+
import { isSignedInAtom, userNameAtom } from '@baca/store/auth'
47+
48+
export const UserName = () => {
49+
const isSignedIn = useAtomValue(isSignedInAtom)
50+
const userName = useAtomValue(userNameAtom)
51+
52+
if (isSignedIn) {
53+
return <Text>{userName}</Text>
54+
}
55+
56+
return <Text>No user</Text>
57+
}
58+
```
59+
60+
Get outside of component
61+
62+
```tsx
63+
import { store } from '@baca/store'
64+
import { isSignedInAtom, userNameAtom } from '@baca/store/auth'
65+
66+
const getUserName = () => {
67+
const isSignedIn = store.get(isSignedInAtom)
68+
const userName = store.get(userNameAtom)
69+
70+
if (isSignedIn) {
71+
return userName
72+
}
73+
74+
return null
75+
}
76+
```
77+
78+
### Update atom value
79+
80+
Update with hook
81+
82+
```tsx
83+
import { isSignedInAtom } from '@baca/store/auth'
84+
85+
const SignInButton = () => {
86+
// Optionbally you can use `useSetAtom()`
87+
const [isSignedIn, setIsSignedIn] = useAtom(isSignedInAtom)
88+
89+
const handleSignIn = () => {
90+
// Handle logic on backend
91+
92+
setIsSignedIn(true)
93+
}
94+
95+
if (isSignedIn) {
96+
return null
97+
}
98+
99+
return <Button onPress={handleSignIn}>Sign in</Button>
100+
}
101+
```
102+
103+
Update outside of component
104+
105+
```tsx
106+
import { store } from '@baca/store'
107+
import { isSignedInAtom } from '@baca/store/auth'
108+
109+
const handleSignIn = () => {
110+
// Handle logic on backend
111+
112+
store.set(isSignedInAtom, true)
113+
}
114+
115+
const SignInButton = () => {
116+
const isSignedIn = useAtomValue(isSignedInAtom)
117+
118+
if (isSignedIn) {
119+
return null
120+
}
121+
122+
return <Button onPress={handleSignIn}>Sign in</Button>
123+
}
124+
```

0 commit comments

Comments
 (0)