comparison.md
This file
Path
docs/learn/getting-started/comparison.md
483 lines · 9.7 KiB · Markdown
Last changed by
Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9
History of this fileTake it
Revamping docs - file restructuring (#3392)Danilo Brittobefore adoption8eae4e9
lines 1–483why, one step back1---2title: Comparison3description: How Zustand stacks up against similar libraries4nav: 25---67Zustand is one of many state management libraries for React.8On this page we will discuss Zustand9in comparison to some of these libraries,10including Redux, Valtio, Jotai, and Recoil.1112Each library has its own strengths and weaknesses,13and we will compare key differences and similarities between each.1415## Redux1617### State Model (vs Redux)1819Conceptually, Zustand and Redux are quite similar,20both are based on an immutable state model.21However, Redux requires your app to be wrapped22in context providers; Zustand does not.2324**Zustand**2526```ts27import { create } from 'zustand'2829type State = {30 count: number31}3233type Actions = {34 increment: (qty: number) => void35 decrement: (qty: number) => void36}3738const useCountStore = create<State & Actions>((set) => ({39 count: 0,40 increment: (qty: number) => set((state) => ({ count: state.count + qty })),41 decrement: (qty: number) => set((state) => ({ count: state.count - qty })),42}))43```4445```ts46import { create } from 'zustand'4748type State = {49 count: number50}5152type Action = {53 type: 'increment' | 'decrement'54 qty: number55}5657type Actions = {58 dispatch: (action: Action) => void59}6061const countReducer = (state: State, action: Action) => {62 switch (action.type) {63 case 'increment':64 return { count: state.count + action.qty }65 case 'decrement':66 return { count: state.count - action.qty }67 default:68 return state69 }70}7172const useCountStore = create<State & Actions>((set) => ({73 count: 0,74 dispatch: (action: Action) => set((state) => countReducer(state, action)),75}))76```7778**Redux**7980```ts81import { createStore } from 'redux'82import { useSelector, useDispatch } from 'react-redux'8384type State = {85 count: number86}8788type Action = {89 type: 'increment' | 'decrement'90 qty: number91}9293const countReducer = (state: State, action: Action) => {94 switch (action.type) {95 case 'increment':96 return { count: state.count + action.qty }97 case 'decrement':98 return { count: state.count - action.qty }99 default:100 return state101 }102}103104const countStore = createStore(countReducer)105```106107```ts108import { createSlice, configureStore } from '@reduxjs/toolkit'109110const countSlice = createSlice({111 name: 'count',112 initialState: { value: 0 },113 reducers: {114 incremented: (state, qty: number) => {115 // Redux Toolkit does not mutate the state, it uses the Immer library116 // behind scenes, allowing us to have something called "draft state".117 state.value += qty118 },119 decremented: (state, qty: number) => {120 state.value -= qty121 },122 },123})124125const countStore = configureStore({ reducer: countSlice.reducer })126```127128### Render Optimization (vs Redux)129130When it comes to render optimizations within your app,131there are no major differences in approach between Zustand and Redux.132In both libraries it is recommended133that you manually apply render optimizations by using selectors.134135**Zustand**136137```ts138import { create } from 'zustand'139140type State = {141 count: number142}143144type Actions = {145 increment: (qty: number) => void146 decrement: (qty: number) => void147}148149const useCountStore = create<State & Actions>((set) => ({150 count: 0,151 increment: (qty: number) => set((state) => ({ count: state.count + qty })),152 decrement: (qty: number) => set((state) => ({ count: state.count - qty })),153}))154155const Component = () => {156 const count = useCountStore((state) => state.count)157 const increment = useCountStore((state) => state.increment)158 const decrement = useCountStore((state) => state.decrement)159 // ...160}161```162163**Redux**164165```ts166import { createStore } from 'redux'167import { useSelector, useDispatch } from 'react-redux'168169type State = {170 count: number171}172173type Action = {174 type: 'increment' | 'decrement'175 qty: number176}177178const countReducer = (state: State, action: Action) => {179 switch (action.type) {180 case 'increment':181 return { count: state.count + action.qty }182 case 'decrement':183 return { count: state.count - action.qty }184 default:185 return state186 }187}188189const countStore = createStore(countReducer)190191const Component = () => {192 const count = useSelector((state) => state.count)193 const dispatch = useDispatch()194 // ...195}196```197198```ts199import { useSelector } from 'react-redux'200import type { TypedUseSelectorHook } from 'react-redux'201import { createSlice, configureStore } from '@reduxjs/toolkit'202203const countSlice = createSlice({204 name: 'count',205 initialState: { value: 0 },206 reducers: {207 incremented: (state, qty: number) => {208 // Redux Toolkit does not mutate the state, it uses the Immer library209 // behind scenes, allowing us to have something called "draft state".210 state.value += qty211 },212 decremented: (state, qty: number) => {213 state.value -= qty214 },215 },216})217218const countStore = configureStore({ reducer: countSlice.reducer })219220const useAppSelector: TypedUseSelectorHook<typeof countStore.getState> =221 useSelector222223const useAppDispatch: () => typeof countStore.dispatch = useDispatch224225const Component = () => {226 const count = useAppSelector((state) => state.count.value)227 const dispatch = useAppDispatch()228 // ...229}230```231232## Valtio233234### State Model (vs Valtio)235236Zustand and Valtio approach state management237in a fundamentally different way.238Zustand is based on the **immutable** state model,239while Valtio is based on the **mutable** state model.240241**Zustand**242243```ts244import { create } from 'zustand'245246type State = {247 obj: { count: number }248}249250const store = create<State>(() => ({ obj: { count: 0 } }))251252store.setState((prev) => ({ obj: { count: prev.obj.count + 1 } }))253```254255**Valtio**256257```ts258import { proxy } from 'valtio'259260const state = proxy({ obj: { count: 0 } })261262state.obj.count += 1263```264265### Render Optimization (vs Valtio)266267The other difference between Zustand and Valtio268is Valtio makes render optimizations through property access.269However, with Zustand, it is recommended that270you manually apply render optimizations by using selectors.271272**Zustand**273274```ts275import { create } from 'zustand'276277type State = {278 count: number279}280281const useCountStore = create<State>(() => ({282 count: 0,283}))284285const Component = () => {286 const count = useCountStore((state) => state.count)287 // ...288}289```290291**Valtio**292293```ts294import { proxy, useSnapshot } from 'valtio'295296const state = proxy({297 count: 0,298})299300const Component = () => {301 const { count } = useSnapshot(state)302 // ...303}304```305306## Jotai307308### State Model (vs Jotai)309310There is one major difference between Zustand and Jotai.311Zustand is a single store,312while Jotai consists of primitive atoms that can be composed together.313314**Zustand**315316```ts317import { create } from 'zustand'318319type State = {320 count: number321}322323type Actions = {324 updateCount: (325 countCallback: (count: State['count']) => State['count'],326 ) => void327}328329const useCountStore = create<State & Actions>((set) => ({330 count: 0,331 updateCount: (countCallback) =>332 set((state) => ({ count: countCallback(state.count) })),333}))334```335336**Jotai**337338```ts339import { atom } from 'jotai'340341const countAtom = atom<number>(0)342```343344### Render Optimization (vs Jotai)345346Jotai achieves render optimizations through atom dependency.347However, with Zustand it is recommended that348you manually apply render optimizations by using selectors.349350**Zustand**351352```ts353import { create } from 'zustand'354355type State = {356 count: number357}358359type Actions = {360 updateCount: (361 countCallback: (count: State['count']) => State['count'],362 ) => void363}364365const useCountStore = create<State & Actions>((set) => ({366 count: 0,367 updateCount: (countCallback) =>368 set((state) => ({ count: countCallback(state.count) })),369}))370371const Component = () => {372 const count = useCountStore((state) => state.count)373 const updateCount = useCountStore((state) => state.updateCount)374 // ...375}376```377378**Jotai**379380```ts381import { atom, useAtom } from 'jotai'382383const countAtom = atom<number>(0)384385const Component = () => {386 const [count, updateCount] = useAtom(countAtom)387 // ...388}389```390391## Recoil392393### State Model (vs Recoil)394395The difference between Zustand and Recoil396is similar to that between Zustand and Jotai.397Recoil depends on atom string keys398instead of atom object referential identities.399Additionally, Recoil needs to wrap your app in a context provider.400401**Zustand**402403```ts404import { create } from 'zustand'405406type State = {407 count: number408}409410type Actions = {411 setCount: (countCallback: (count: State['count']) => State['count']) => void412}413414const useCountStore = create<State & Actions>((set) => ({415 count: 0,416 setCount: (countCallback) =>417 set((state) => ({ count: countCallback(state.count) })),418}))419```420421**Recoil**422423```ts424import { atom } from 'recoil'425426const count = atom({427 key: 'count',428 default: 0,429})430```431432### Render Optimization (vs Recoil)433434Similar to previous optimization comparisons,435Recoil makes render optimizations through atom dependency.436Whereas with Zustand, it is recommended that437you manually apply render optimizations by using selectors.438439**Zustand**440441```ts442import { create } from 'zustand'443444type State = {445 count: number446}447448type Actions = {449 setCount: (countCallback: (count: State['count']) => State['count']) => void450}451452const useCountStore = create<State & Actions>((set) => ({453 count: 0,454 setCount: (countCallback) =>455 set((state) => ({ count: countCallback(state.count) })),456}))457458const Component = () => {459 const count = useCountStore((state) => state.count)460 const setCount = useCountStore((state) => state.setCount)461 // ...462}463```464465**Recoil**466467```ts468import { atom, useRecoilState } from 'recoil'469470const countAtom = atom({471 key: 'count',472 default: 0,473})474475const Component = () => {476 const [count, setCount] = useRecoilState(countAtom)477 // ...478}479```480481## Npm Downloads Trend482483- [Npm Downloads Trend of State Management Libraries for React](https://npm-stat.com/charts.html?package=zustand&package=jotai&package=valtio&package=%40reduxjs%2Ftoolkit&package=recoil)