├── .gitignore ├── README.md ├── components ├── clock.tsx ├── counter.tsx ├── nav.tsx └── page.tsx ├── lib ├── StoreProvider.tsx ├── store.ts └── useInterval.ts ├── next-env.d.ts ├── package.json ├── pages ├── _app.tsx ├── index.tsx ├── ssg.tsx └── ssr.tsx ├── tsconfig.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # next.js 12 | /.next/ 13 | /out/ 14 | 15 | # production 16 | /build 17 | 18 | # misc 19 | .DS_Store 20 | *.pem 21 | 22 | # debug 23 | npm-debug.log* 24 | yarn-debug.log* 25 | yarn-error.log* 26 | 27 | # local env files 28 | .env.local 29 | .env.development.local 30 | .env.test.local 31 | .env.production.local 32 | 33 | # vercel 34 | .vercel 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Zustand example with Typescript 2 | 3 | This example shows how to integrate Zustand in Next.js with typescript. 4 | You dont need to manually define StoreInterface. It will get infer by the combine function from zustand. 5 | It helps to auto suggest what states are in store. When it comes to big project, Typescript really helps 6 | It is modified from original js version https://github.com/vercel/next.js/tree/master/examples/with-zustand 7 | 8 | Usually splitting your app state into `pages` feels natural but sometimes you'll want to have global state for your app. This is an example on how you can use Zustand that also works with Next.js's universal rendering approach. 9 | 10 | In the first example we are going to display a digital clock that updates every second. The first render is happening in the server and then the browser will take over. To illustrate this, the server rendered clock will have a different background color (black) than the client one (grey). 11 | 12 | To illustrate SSG and SSR, go to `/ssg` and `/ssr`, those pages are using Next.js data fetching methods to get the date in the server and return it as props to the page, and then the browser will hydrate the store and continue updating the date. 13 | 14 | The trick here for supporting universal Zustand is to separate the cases for the client and the server. When we are on the server we want to create a new store every time, otherwise different users data will be mixed up. If we are in the client we want to use always the same store. That's what we accomplish on `store.js`. 15 | 16 | All components have access to the Zustand store using `useStore()` returned from zustand's `createContext()` function. 17 | 18 | On the server side every request initializes a new store, because otherwise different user data can be mixed up. On the client side the same store is used, even between page changes. 19 | 20 | ## How to use 21 | 22 | Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example: 23 | 24 | ```bash 25 | git clone 26 | yarn 27 | yarn dev 28 | ``` 29 | -------------------------------------------------------------------------------- /components/clock.tsx: -------------------------------------------------------------------------------- 1 | import { useStore } from "../lib/store"; 2 | 3 | const useClock = () => { 4 | return useStore((store) => ({ 5 | lastUpdate: store.lastUpdate, 6 | light: store.light, 7 | })); 8 | }; 9 | 10 | const formatTime = (time: number) => { 11 | // cut off except hh:mm:ss 12 | return new Date(time).toJSON().slice(11, 19); 13 | }; 14 | 15 | const Clock = () => { 16 | const { lastUpdate, light } = useClock(); 17 | return ( 18 |
19 | {formatTime(lastUpdate)} 20 | 33 |
34 | ); 35 | }; 36 | 37 | export default Clock; 38 | -------------------------------------------------------------------------------- /components/counter.tsx: -------------------------------------------------------------------------------- 1 | import { useStore } from "../lib/store"; 2 | const useCounter = () => { 3 | const { count, increment, decrement, reset } = useStore((store) => ({ 4 | count: store.count, 5 | increment: store.increment, 6 | decrement: store.decrement, 7 | reset: store.reset, 8 | })); 9 | 10 | return { count, increment, decrement, reset }; 11 | }; 12 | 13 | const Counter = () => { 14 | const { count, increment, decrement, reset } = useCounter(); 15 | return ( 16 |
17 |

18 | Count: {count} 19 |

20 | 21 | 22 | 23 |
24 | ); 25 | }; 26 | 27 | export default Counter; 28 | -------------------------------------------------------------------------------- /components/nav.tsx: -------------------------------------------------------------------------------- 1 | import Link from "next/link"; 2 | 3 | const Nav = () => { 4 | return ( 5 | 17 | ); 18 | }; 19 | 20 | export default Nav; 21 | -------------------------------------------------------------------------------- /components/page.tsx: -------------------------------------------------------------------------------- 1 | import useInterval from "../lib/useInterval"; 2 | import Clock from "./clock"; 3 | import Counter from "./counter"; 4 | import Nav from "./nav"; 5 | import { useStore } from "../lib/store"; 6 | 7 | export default function Page() { 8 | const tick = useStore((store) => store.tick); 9 | 10 | // Tick the time every second 11 | useInterval(() => { 12 | tick(Date.now(), true); 13 | }, 1000); 14 | 15 | return ( 16 | <> 17 |