From adce5e6f2adff35994b8b9c9d04aa6a6dbee5e77 Mon Sep 17 00:00:00 2001 From: Usman Sabuwala <51731966+max-programming@users.noreply.github.com> Date: Fri, 5 Dec 2025 19:26:42 +0530 Subject: [PATCH] docs: remove duplicate code sections (#3310) --- docs/guides/nextjs.md | 89 +------------------------------------------ 1 file changed, 2 insertions(+), 87 deletions(-) diff --git a/docs/guides/nextjs.md b/docs/guides/nextjs.md index e0e70857..6cc3ed76 100644 --- a/docs/guides/nextjs.md +++ b/docs/guides/nextjs.md @@ -71,6 +71,8 @@ request. > **Note:** do not forget to remove all comments from your `tsconfig.json` file. +### Initializing the store + ```ts // src/stores/counter-store.ts import { createStore } from 'zustand/vanilla' @@ -158,93 +160,6 @@ export const useCounterStore = ( > there are stateful client components located above this component in the tree, or if this component > also contains other mutable state that causes a re-render. -### Initializing the store - -```ts -// src/stores/counter-store.ts -import { createStore } from 'zustand/vanilla' - -export type CounterState = { - count: number -} - -export type CounterActions = { - decrementCount: () => void - incrementCount: () => void -} - -export type CounterStore = CounterState & CounterActions - -export const initCounterStore = (): CounterState => { - return { count: new Date().getFullYear() } -} - -export const defaultInitState: CounterState = { - count: 0, -} - -export const createCounterStore = ( - initState: CounterState = defaultInitState, -) => { - return createStore()((set) => ({ - ...initState, - decrementCount: () => set((state) => ({ count: state.count - 1 })), - incrementCount: () => set((state) => ({ count: state.count + 1 })), - })) -} -``` - -```tsx -// src/providers/counter-store-provider.tsx -'use client' - -import { type ReactNode, createContext, useRef, useContext } from 'react' -import { useStore } from 'zustand' - -import { - type CounterStore, - createCounterStore, - initCounterStore, -} from '@/stores/counter-store' - -export type CounterStoreApi = ReturnType - -export const CounterStoreContext = createContext( - undefined, -) - -export interface CounterStoreProviderProps { - children: ReactNode -} - -export const CounterStoreProvider = ({ - children, -}: CounterStoreProviderProps) => { - const storeRef = useRef(null) - if (storeRef.current === null) { - storeRef.current = createCounterStore(initCounterStore()) - } - - return ( - - {children} - - ) -} - -export const useCounterStore = ( - selector: (store: CounterStore) => T, -): T => { - const counterStoreContext = useContext(CounterStoreContext) - - if (!counterStoreContext) { - throw new Error(`useCounterStore must be used within CounterStoreProvider`) - } - - return useStore(counterStoreContext, selector) -} -``` - ### Using the store with different architectures There are two architectures for a Next.js application: the