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
Provenance
- Commit
adce5e6- Tree
c187caf- Author
Usman Sabuwala <51731966+max-programming@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
32a3698
1 file changed+2 added−87 removed
modified:docs/guides/nextjs.md+2 added-87 removed
69 unmodified lines70 ```71 72 > **Note:** do not forget to remove all comments from your `tsconfig.json` file.73 74+### Initializing the store75+76 ```ts77 // src/stores/counter-store.ts78 import { createStore } from 'zustand/vanilla'79 79 unmodified lines159 > 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.162 161-### Initializing the store162-163-```ts164-// src/stores/counter-store.ts165-import { createStore } from 'zustand/vanilla'166-167-export type CounterState = {168- count: number169-}170-171-export type CounterActions = {172- decrementCount: () => void173- incrementCount: () => void174-}175-176-export type CounterStore = CounterState & CounterActions177-178-export const initCounterStore = (): CounterState => {179- return { count: new Date().getFullYear() }180-}181-182-export const defaultInitState: CounterState = {183- count: 0,184-}185-186-export 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-```196-197-```tsx198-// src/providers/counter-store-provider.tsx199-'use client'200-201-import { type ReactNode, createContext, useRef, useContext } from 'react'202-import { useStore } from 'zustand'203-204-import {205- type CounterStore,206- createCounterStore,207- initCounterStore,208-} from '@/stores/counter-store'209-210-export type CounterStoreApi = ReturnType<typeof createCounterStore>211-212-export const CounterStoreContext = createContext<CounterStoreApi | undefined>(213- undefined,214-)215-216-export interface CounterStoreProviderProps {217- children: ReactNode218-}219-220-export const CounterStoreProvider = ({221- children,222-}: CounterStoreProviderProps) => {223- const storeRef = useRef<CounterStoreApi | null>(null)224- if (storeRef.current === null) {225- storeRef.current = createCounterStore(initCounterStore())226- }227-228- return (229- <CounterStoreContext.Provider value={storeRef.current}>230- {children}231- </CounterStoreContext.Provider>232- )233-}234-235-export const useCounterStore = <T,>(236- selector: (store: CounterStore) => T,237-): T => {238- const counterStoreContext = useContext(CounterStoreContext)239-240- if (!counterStoreContext) {241- throw new Error(`useCounterStore must be used within CounterStoreProvider`)242- }243-244- return useStore(counterStoreContext, selector)245-}246-```247-163 ### Using the store with different architectures164 165 There are two architectures for a Next.js application: the166 [Pages Router](https://nextjs.org/docs/pages/building-your-application/routing) and the155 unmodified lines