-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
53 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,40 @@ | ||
export { useStore } | ||
|
||
import { create, server, withPageContext } from 'vike-react-zustand' | ||
import { create, serverOnly, withPageContext } from 'vike-react-zustand' | ||
|
||
interface Store { | ||
counter: number | ||
setCounter: (value: number) => void | ||
serverEnv: string | ||
url: string | ||
} | ||
|
||
const useStore = create<Store>()( | ||
( | ||
set, | ||
get | ||
/* TODO | ||
pageContext | ||
*/ | ||
) => ({ | ||
const useStore = withPageContext((pageContext) => | ||
create<Store>()((set, get) => ({ | ||
counter: Math.floor(10000 * Math.random()), | ||
setCounter(value) { | ||
set({ counter: value }) | ||
}, | ||
url: pageContext.urlOriginal, | ||
|
||
// the callback only runs on the server, | ||
// the return value is passed to the client on the initial navigation | ||
...server(() => ({ | ||
...serverOnly(() => ({ | ||
serverEnv: process.env.SOME_ENV! | ||
})) | ||
}) | ||
})) | ||
) | ||
|
||
// This works, too | ||
// const useStore = create<Store>()((set, get) => ({ | ||
// counter: Math.floor(10000 * Math.random()), | ||
// setCounter(value) { | ||
// set({ counter: value }) | ||
// }, | ||
|
||
// // the callback only runs on the server, | ||
// // the return value is passed to the client on the initial navigation | ||
// ...server(() => ({ | ||
// serverEnv: process.env.SOME_ENV! | ||
// })) | ||
// })) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
export { getContext, getCreateStore, setCreateStore } | ||
|
||
import React, { createContext } from 'react' | ||
import { getGlobalObject } from '../utils.js' | ||
import type { create } from 'zustand' | ||
|
||
type StoreAndHook = ReturnType<typeof create> | ||
type CreateStore = () => StoreAndHook & { __hydrated__?: true } | ||
type CreateStore = (pageContext: any) => StoreAndHook & { __hydrated__?: true } | ||
|
||
const globalObject = getGlobalObject('VikeReactZustandContext.ts', { | ||
createStore: undefined as CreateStore | undefined, | ||
context: createContext<StoreAndHook | undefined>(undefined) | ||
}) | ||
|
||
export const getCreateStore = () => globalObject.createStore | ||
export const getContext = () => globalObject.context as unknown as React.Context<StoreAndHook | undefined> | ||
const getContext = () => globalObject.context as unknown as React.Context<StoreAndHook | undefined> | ||
|
||
export const setCreateStore = (createStore_: CreateStore) => { | ||
const getCreateStore = () => globalObject.createStore | ||
const setCreateStore = (createStore_: CreateStore) => { | ||
globalObject.createStore = createStore_ | ||
} |