修复环境变量问题
This commit is contained in:
38
src/components/stores/app.tsx
Normal file
38
src/components/stores/app.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
'use client'
|
||||
import {createStore, StoreApi} from 'zustand/vanilla'
|
||||
import {createContext, ReactNode, useContext, useState} from 'react'
|
||||
import {useStore} from 'zustand/react'
|
||||
|
||||
// store
|
||||
type AppStoreState = {
|
||||
apiUrl: string
|
||||
}
|
||||
|
||||
export function createAppStore(url: string) {
|
||||
return createStore<AppStoreState>()(() => ({
|
||||
apiUrl: url,
|
||||
}))
|
||||
}
|
||||
|
||||
// provider
|
||||
const AppStoreContext = createContext<StoreApi<AppStoreState> | null>(null)
|
||||
|
||||
export function AppStoreProvider(props: {
|
||||
url: string
|
||||
children: ReactNode
|
||||
}) {
|
||||
const [store] = useState(() => createAppStore(props.url))
|
||||
return (
|
||||
<AppStoreContext.Provider value={store}>
|
||||
{props.children}
|
||||
</AppStoreContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function useAppStore(name: keyof AppStoreState) {
|
||||
const context = useContext(AppStoreContext)
|
||||
if (!context) {
|
||||
throw new Error('AppStoreContext 没有正确初始化')
|
||||
}
|
||||
return useStore(context, c => c[name])
|
||||
}
|
||||
Reference in New Issue
Block a user