-
Zustand, like React, expects immutable data structures for state management. However, here’s the context of my requirements:
Here are two specific scenarios to illustrate why I have this unusual need:
Currently, I use closures in slice creators to handle such scenarios, like this: export const createCounterSlice = (set, get) => {
let nonReactiveArray = [];
return {
nonReactiveArray,
// Non-reactive, accessible by other slices but not intended for selectors.
// In most cases, it doesn’t need to be exposed here.
length: 0,
// Reactive array length.
push: (value) => {
nonReactiveArray.push(value); // Mutates in-place.
set(state => {
state.length += 1; // Reactive update with immer.
});
},
};
}; This works but feels misaligned with Zustand’s philosophy. What would be the recommended approach for my use cases in Zustand? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
What's documented is transient updates. I think what you are doing is fine as long as you understand it correctly, and I can see that from the comment. But, yeah, I wouldn't put it in the store as you implied.
What's the case you need to put it in the store? |
Beta Was this translation helpful? Give feedback.
What's documented is transient updates.
I think what you are doing is fine as long as you understand it correctly, and I can see that from the comment. But, yeah, I wouldn't put it in the store as you implied.
What's the case you need to put it in the store?