The type of setState
is a object when it is built and deployed to Cloudflare pages while locally it is a function.
#2548
-
I'm working on a small project using Next.js with Zustand for state management. According to the Zustand documentation, I've implemented the recommended slices pattern for managing the state of the homepage and used a React Context Provider to initialize the state per request on the client side. Everything functions correctly in the local environment. However, upon deploying to Cloudflare Pages, the application encounters the following error in production:
Locally, the function causing the error works as expected, but in production, it's incorrectly recognized as an object. I'm unsure why this discrepancy occurs between the local build and the production environment. The repository with the code is available here: KamaniBhavin/pitch-black-ui To be specific about the part where the error occurs: https://github.com/KamaniBhavin/pitch-black-ui/blob/4572b9bb4a0548dfc7312f37f392b3ca1e34de2f/src/stores/home-page-store/chat-window-slice.ts#L39 Any insights or suggestions to resolve this issue would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The |
Beta Was this translation helpful? Give feedback.
-
It turns out the issue was related to how I configured the slices. Initially, I set up the slices as a function that returned a state creator, as illustrated in the incorrect example below: ❌
function createSliceA(): StateCreator<...> {
return (set, get) => ({
})
}
export function store() {
return createStore<TStore>()(
immer((...args) => ({
...createSliceA()(...args),
...createSliceB()(...args),
})),
);
} The correct configuration involves defining the slices directly as functions, which fixed the problem. Here's the corrected code: ✅
const createSliceA = (set, get) => ({
})
export function store() {
return createStore<TStore>()(
immer((...args) => ({
...createSliceA(...args),
...createSliceB(...args),
})),
);
} |
Beta Was this translation helpful? Give feedback.
It turns out the issue was related to how I configured the slices. Initially, I set up the slices as a function that returned a state creator, as illustrated in the incorrect example below:
The correct configuration involves defining the slices directly as functions, which fixed the problem. Here's the corrected code: