- Let's compare Redux Toolkit and Zustand in Next.js 14 using basic counter as of July 2024.
- I tried to create very simple counter based on (i.e. copy paste from) each official docs.
- please refer also npm trends
npm ci
npm run dev
# with port number
npm run dev -- -p 3030
Redux | Zustand | Winner | |
---|---|---|---|
setup package | official doc is not clear what's required. you will need react-redux as well as @reduxjs/toolkit |
just zustand |
Zustand |
coding experience | official doc is not so friendly so you will need to google a lot |
official doc provides clear info | Zustand |
readability | still mysterious boilerplate required even in Redux Toolkit cf. src/redux-lib/hooks.ts |
you just need one interface and useStore custom hook |
Zustand |
number of lines | 38 lines cf. counterSlice.ts |
7 lines cf. useCounterStore.ts |
Zustand |
impression | always come with dispatch looks not DRY nowadaysneed to wrap children by <StoreProvider> in root layout.tsx almost like Context |
optional to use a Context as in CounterStoreProvider.tsx |
Zustand |
- Redux
- Zustand
- Zustand with Context
- Both will need
"use client";
since it's about the state of the client side. - Zustand store is optional to use Context
- But to reset a store, need to initialize the store at the component level using a Context
- Overall, Zustand is much simpler to create basic stuff like counter.
- if it's more than counter, you may need to consider other factors.
- Next.js
- https://nextjs.org/docs/getting-started/installation
npx create-next-app@latest
-
Redux Toolkit
- https://redux-toolkit.js.org/introduction/getting-started
npx create-next-app --example with-redux my-app
is not recommended- it will create old style app.
npm install @reduxjs/toolkit
- https://redux-toolkit.js.org/introduction/getting-started
-
React Redux
- https://react-redux.js.org/introduction/getting-started
npm install react-redux
-
Redux Next.js
- https://redux-toolkit.js.org/usage/nextjs
- official doc suggest to put Redux stuff in
src/lib
- Zustand
- https://github.com/pmndrs/zustand
- https://zustand-demo.pmnd.rs/
npm install zustand
- https://nextui.org/docs/guide/installation
npm install @nextui-org/react framer-motion
- https://github.com/tailwindlabs/tailwindcss-typography
npm install -D @tailwindcss/typography
- https://github.com/lydell/eslint-plugin-simple-import-sort
npm install --save-dev eslint-plugin-simple-import-sort