(1) Zustand vs Jotai
プロダクトの中で生まれた課題と提案
21
Zustand
Jotai
const countAtom = atom(0)
const textAtom = atom('')
const flagAtom = atom(false)
function SampleComponent() {
const [count, setCount] = useAtom(countAtom)
const [text, setText] = useAtom(textAtom)
return (
{count}
setCount(c => c + 1)}>+1
setText(e.target.value)} />
)
}
const useStore = create((set) => ({
count: 0,
text: '',
flag: false,
inc: () => set(s => ({ count: s.count + 1 })),
setText: (text) => set(() => ({ text })),
toggleFlag: () => set(s => ({ flag: !s.flag })),
}))
function SampleComponent() {
const { count, text, inc, setText } = useStore(useShallow((state) => {
count: state.count,
text: state.text,
inc: state.inc,
setText: state.setText
}
))
return (
{count}
+1
setText(e.target.value)} />
)
}
● ZustandはuseReducer、JotaiはuseStateに
コード設計が近い
● Zustandはセレクタもstoreのなかで管理できる
→ Zustandを選択