-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: no more global store on nextjs :v
- Loading branch information
Showing
9 changed files
with
254 additions
and
188 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
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
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
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use client'; | ||
|
||
import { type State, type Store, createStore } from '@/store'; | ||
import { type ReactNode, createContext, useContext, useRef } from 'react'; | ||
import { type TemporalState } from 'zundo'; | ||
import { useStore as useZustandStore } from 'zustand'; | ||
|
||
export const StoreContext = createContext<ReturnType< | ||
typeof createStore | ||
> | null>(null); | ||
|
||
export function StoreProvider({ children }: { children: ReactNode }) { | ||
const storeRef = useRef<ReturnType<typeof createStore>>(); | ||
if (!storeRef.current) { | ||
storeRef.current = createStore(); | ||
} | ||
return ( | ||
<StoreContext.Provider value={storeRef.current}> | ||
{children} | ||
</StoreContext.Provider> | ||
); | ||
} | ||
|
||
export const useStore = <T,>(selector: (state: Store) => T): T => { | ||
const store = useContext(StoreContext); | ||
if (!store) { | ||
throw new Error('useStore must be used within a StoreProvider'); | ||
} | ||
return useZustandStore(store, selector); | ||
}; | ||
|
||
export const useTemporalStore = <T,>( | ||
selector: (state: TemporalState<Partial<State>>) => T, | ||
): T => { | ||
const store = useContext(StoreContext); | ||
if (!store) { | ||
throw new Error('useTemporalStore must be used within a StoreProvider'); | ||
} | ||
return useZustandStore(store.temporal, selector); | ||
}; |
Oops, something went wrong.