Skip to content
Myr

docs: remove duplicate code sections (#3310)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Usman Sabuwala · committed by GitHub · parent 32a3698 · Browse files · 1 file+2 added−87 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+2 added−87 removed

  • other:+2 added−87 removed

1 file+2 added−87 removed

modified:docs/guides/nextjs.md+2 added-87 removed
69 unmodified lines
70```7172> **Note:** do not forget to remove all comments from your `tsconfig.json` file.7374### Initializing the store7576```ts77// src/stores/counter-store.ts78import { createStore } from 'zustand/vanilla'79
79 unmodified lines
159> rendered once per request on the server, but might be re-rendered multiple times on the client if160> there are stateful client components located above this component in the tree, or if this component161> also contains other mutable state that causes a re-render.162161### Initializing the store162163```ts164// src/stores/counter-store.ts165import { createStore } from 'zustand/vanilla'166167export type CounterState = {168 count: number169}170171export type CounterActions = {172 decrementCount: () => void173 incrementCount: () => void174}175176export type CounterStore = CounterState & CounterActions177178export const initCounterStore = (): CounterState => {179 return { count: new Date().getFullYear() }180}181182export const defaultInitState: CounterState = {183 count: 0,184}185186export const createCounterStore = (187 initState: CounterState = defaultInitState,188) => {189 return createStore<CounterStore>()((set) => ({190 ...initState,191 decrementCount: () => set((state) => ({ count: state.count - 1 })),192 incrementCount: () => set((state) => ({ count: state.count + 1 })),193 }))194}195```196197```tsx198// src/providers/counter-store-provider.tsx199'use client'200201import { type ReactNode, createContext, useRef, useContext } from 'react'202import { useStore } from 'zustand'203204import {205 type CounterStore,206 createCounterStore,207 initCounterStore,208} from '@/stores/counter-store'209210export type CounterStoreApi = ReturnType<typeof createCounterStore>211212export const CounterStoreContext = createContext<CounterStoreApi | undefined>(213 undefined,214)215216export interface CounterStoreProviderProps {217 children: ReactNode218}219220export const CounterStoreProvider = ({221 children,222}: CounterStoreProviderProps) => {223 const storeRef = useRef<CounterStoreApi | null>(null)224 if (storeRef.current === null) {225 storeRef.current = createCounterStore(initCounterStore())226 }227228 return (229 <CounterStoreContext.Provider value={storeRef.current}>230 {children}231 </CounterStoreContext.Provider>232 )233}234235export const useCounterStore = <T,>(236 selector: (store: CounterStore) => T,237): T => {238 const counterStoreContext = useContext(CounterStoreContext)239240 if (!counterStoreContext) {241 throw new Error(`useCounterStore must be used within CounterStoreProvider`)242 }243244 return useStore(counterStoreContext, selector)245}246```247163### Using the store with different architectures164165There are two architectures for a Next.js application: the166[Pages Router](https://nextjs.org/docs/pages/building-your-application/routing) and the
155 unmodified lines