β Information
- defining zustand store
β Relevant data
π¦ Information Resources
π° Content ->
Store μμ±
create<νμ
>()import { create } from 'zustand'
export const useCountStore = create<{
count: number
increase: () => void
decrease: () => void
}>(set => ({
count: 1,
increase: () => set(state => ({ count: state.count + 1 })),
decrease: () => set(state => ({ count: state.count - 1 }))
}))Store μ¬μ©
μ‘μ μ¬μ© λ°©μ
useCountStore(...) λ°©μμ΄ μκ³ , useCountStore.getState() λ°©μμ΄ μλ€.
λ λ€ store μ κΈ°λ₯μ μ μμ μΌλ‘ μ¬μ©νλ κ²μ΄λ€.
κ·Έλ°λ° μ»΄ν¬λνΈ λ΄λΆμμλ useCountStore(...)λ₯Ό μ¬μ©νλ€.
μ΄μ λ μ»΄ν¬λνΈμ 리λ λλ§μ μν΄μμ΄λ€.
useUserStore.getState() λ νμ¬ μν κ°μ²΄λ₯Ό μ¦μ κ°μ Έμ€κΈ° λλ¬Έμ κ°μ΄ λ°λμ΄λ 리λ λλ§μ΄ μλλ€
λ°λΌμ λΉUI λ‘μ§ μμλ useCountStore.getState() λ₯Ό μ¬μ©νλ κ²μ΄λ€.
λΆκ°μ ν¬μΈνΈ
- zustandλ λ΄λΆμ μΌλ‘ vanilla storeλ₯Ό μ 곡νκ³ ,
useUserStoreλ κ·Έ μμ React Hookμ μ ν κ² - κ·Έλμ λ λλ§ μ°κ²° μ¬λΆμ λ°λΌ λ κ°μ§ λ°©μ μ€ μ ννλ κ±°μ§, κΈ°λ₯μ μΌλ‘λ κ°μ storeλ₯Ό λ°λΌλ³΄κ³ μμ
μ‘μ νλμ© κ°μ Έμ€κΈ°
store λ₯Ό μ¬μ©νλ λͺ¨λ μ»΄ν¬λνΈμ λΆνμν 리λ λλ§μ λ°©μ§νκΈ° μν΄, μ»΄ν¬λνΈμμλ ν λ²μ νλμ©λ§ μν(μ‘μ )μ κ°μ ΈμμΌν¨
import { useCountStore } from './store/count'
export default function App() {
const count = useCountStore(state => state.count)
const increase = useCountStore(state => state.increase)
const decrease = useCountStore(state => state.decrease)
return (
<>
<h2>{count}</h2>
<button onClick={increase}>+1</button>
<button onClick={decrease}>-1</button>
</>
)
}λ€μ€ μν μ ν(useShallow)
useShallowν
μ μ¬μ©νλ©΄ μ¬λ¬ μν(μ‘μ
)μ ν λ²μ κ°μ²΄λ λ°°μ΄λ‘ κ°μ Έμ¬ μ μλ€
import { useShallow } from 'zustand/shallow'
import { useCountStore } from './store/count'
export default function App() {
// κ°μ²΄
// const { count, increase, decrease } = useCountStore(state => ({
const countState = useCountStore(
useShallow(state => ({
count: state.count,
increase: state.increase,
decrease: state.decrease
}))
)
return (
<>
<h2>{countState.count}</h2>
<button onClick={countState.increase}>+1</button>
<button onClick={countState.decrease}>-1</button>
</>
)
}import { useShallow } from 'zustand/shallow'
import { useCountStore } from './store/count'
export default function App() {
// λ°°μ΄
// const [count, increase, decrease] = useCountStore(
const countState = useCountStore(
useShallow(state => [state.count, state.increase, state.decrease])
)
return (
<>
<h2>{countState[0]}</h2>
<button onClick={countState[1]}>+1</button>
<button onClick={countState[2]}>-1</button>
</>
)
}μ‘μ λΆλ¦¬
μ¬λ¬ μ»΄ν¬λνΈμμ λ¨μΌ μ€ν μ΄μ μ‘μ
μ λ§μ΄ μ¬μ©νλ€λ©΄, μ‘μ
μ λΆλ¦¬ν΄ κ΄λ¦¬νλ ν¨ν΄μ νμ©ν μ μμ.
λ€μκ³Ό κ°μ΄ actions κ°μ²΄ μμμ λͺ¨λ μ‘μ
μ κ΄λ¦¬νλ©΄, κ° μ»΄ν¬λνΈμμ νμν μ‘μ
λ§ κ°μ Έμ€κΈ° μ¬μ
import { create } from 'zustand'
export const useCountStore = create<{
count: number
actions: {
increase: () => void
decrease: () => void
}
}>(set => ({
count: 1,
actions: {
increase: () => set(state => ({ count: state.count + 1 })),
decrease: () => set(state => ({ count: state.count - 1 }))
}
}))import { useCountStore } from './store/count'
export default function App() {
const count = useCountStore(state => state.count)
const { increase, decrease } = useCountStore(state => state.actions)
return (
<>
<h2>{count}</h2>
<button onClick={increase}>+1</button>
<button onClick={decrease}>-1</button>
</>
)
}zustand has middleware devtools, persist
devtools
What it does
From Chrome extention redux-devtools Zustand store can be shown
How to use
- Wrap the create function with devtools as the first parameter.
- Set the store name as the second parameter of devtools.
import create from 'zustand';
import { devtools } from 'zustand/middleware';
const useStore = create(
devtools(
(set) => ({
// ...
}), { name: "CounterStore" }
)
);persist
What it does
Saves state inside browser storage (ex: localStorege, sessionStorage, IndexedDB). So page Refreshing, app restarting wonβt wipe the data.
How to use
- Wrap the
createfunction withpersist - Set the store name, storage to use, etc as the second parameter of devtools.
import create from 'zustand';
import { persist } from 'zustand/middleware';
const useState = create(
persist(
(set) => ({
count: 0,
increment: () => set((state) => ({ count: state.count + 1 })),
}),
{
name: 'count-storage', // μ μ₯μμ μ μ₯λ ν€ μ΄λ¦
storage: createJSONStorage(() => localStorage), // μ¬μ©ν μ μ₯μ
partialize: (store) => ({ count: store.count }). // λ‘컬 μ€ν 리μ§μ μ μ₯ν μνλ§ μ ν
}
)
);