|
| 1 | +--- |
| 2 | +id: jotai |
| 3 | +slug: /jotai |
| 4 | +title: State management - jotai |
| 5 | +sidebar_position: 3 |
| 6 | +tags: |
| 7 | + - Jotai |
| 8 | + - State management |
| 9 | + - React |
| 10 | + - React Native |
| 11 | +description: State management - jotai - |
| 12 | +--- |
| 13 | + |
| 14 | +# State management - Jotai |
| 15 | + |
| 16 | +## Description |
| 17 | + |
| 18 | +This starter comes with jotai state management tool. Please check documentation on how it work in details: |
| 19 | + |
| 20 | +- 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