-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
next js example has typescript error #493
Comments
@Munawwar can you figure it out? |
the second error fixed if defined store undefined-able like this : let store: UseStore<InitialState> | undefined; and this code is fine know useLayoutEffect(() => {
if (initialState && store) {
store.setState({
...store.getState(),
...initialState,
})
}
}, [initialState]) |
when removing type from export const useStore = zustandContext.useStore; throw this error
and on declaration of declare function createContext<TState extends State>(): {
Provider: ({ initialStore, createStore, children, }: {
/**
* @deprecated
*/
initialStore?: UseStore<TState> | undefined;
createStore: () => UseStore<TState>;
children: ReactNode;
}) => import("react").FunctionComponentElement<import("react").ProviderProps<UseStore<TState> | undefined>>;
useStore: UseStoreData<TState>;
useStoreApi: () => {
getState: import("zustand").GetState<TState>;
setState: import("zustand").SetState<TState>;
subscribe: import("zustand").Subscribe<TState>;
destroy: import("zustand").Destroy;
};
}; and the type of it it's not exported
I'm trying to redefine interface on my files but it's throw this error
but fixed when i set generic type of const zustandContext = createContext<InitialState>(); |
know everything is fixed and the whole line of the code is this import { useLayoutEffect } from "react";
import create, {
EqualityChecker,
State,
StateSelector,
UseStore,
} from "zustand/index";
import createContext from "zustand/context";
import {
BlogFirstPage,
blogFirstPageInitial,
blogFirstPageState,
} from "../index";
interface UseStoreData<T extends State> {
(): T;
<U>(selector: StateSelector<T, U>, equalityFn?: EqualityChecker<U>): U;
}
interface InitialState {
blogFirstPage: BlogFirstPage;
}
const initialState: InitialState = {
blogFirstPage: blogFirstPageInitial,
};
let store: UseStore<InitialState> | undefined;
const zustandContext = createContext<InitialState>();
export const Provider = zustandContext.Provider;
// An example of how to get types
/** @type {import('zustand/index').UseStore<typeof initialState>} */
export const useStore: UseStoreData<InitialState> = zustandContext.useStore;
export const initializeStore = (preloadedState = {}) => {
return create<typeof initialState>(blogFirstPageState(preloadedState));
};
export function useHydrate(initialState: InitialState) {
let _store = store ?? initializeStore(initialState);
// For SSR & SSG, always use a new store.
if (typeof window !== "undefined") {
// For CSR, always re-use same store.
if (!store) {
store = _store;
}
// And if initialState changes, then merge states in the next render cycle.
//
// eslint complaining "React Hooks must be called in the exact same order in every component render"
// is ignorable as this code runs in the same order in a given environment (CSR/SSR/SSG)
// eslint-disable-next-line react-hooks/rules-of-hooks
useLayoutEffect(() => {
if (initialState && store) {
store.setState({
...store.getState(),
...initialState,
});
}
}, [initialState]);
}
return _store;
} |
Please try v3.5.7 if you haven't, to see if things change. |
yes it fixed |
it has another type error on Provider : import { useHydrate, Provider } from '../lib/store'
export default function App({ Component, pageProps }) {
const store = useHydrate(pageProps.initialZustandState)
return (
<Provider initialStore={store}>
<Component {...pageProps} />
</Provider>
)
} it's throw this error
I think |
or just change code to this one : export default function App({ Component, pageProps }) {
const store = () => useHydrate(pageProps.initialZustandState)
return (
<Provider createStore={store}>
<Component {...pageProps} />
</Provider>
)
} |
Yeah, initialStore is deprecated. useHydrate should be called in App, so maybe this: export default function App({ Component, pageProps }) {
const store = useHydrate(pageProps.initialZustandState)
return (
<Provider createStore={() => store}>
<Component {...pageProps} />
</Provider>
)
} |
Sorry for the late reply. Didnt see this whole conversation. I opened a PR with next.js to update that example. Latest code you can see at https://github.com/Munawwar/next.js-1/tree/zustand-example and https://codesandbox.io/s/nextjs-with-zustand-354-forked-bryjr?file=/lib/store.js |
this code guide to set type of
useStore
toUseStore<typeof initialState>
with this hint :but it's throw this error
and this chunk of code :
throw this error :
we should call store on condition
The text was updated successfully, but these errors were encountered: