Replies: 10 comments 10 replies
-
I'm not exactly sure the use case, but if it's the same memory space it's shared. If you need isolated stores, you want to create multiple stores. |
Beta Was this translation helpful? Give feedback.
-
wow, thanks for that fast answer. Yes I tried the zustand/context but honestly I did not get it to run. Thanks Pascal |
Beta Was this translation helpful? Give feedback.
-
Does "createContext usage in real components" in readme help? |
Beta Was this translation helpful? Give feedback.
-
Actually not. I do not understand how it works when components and store and in separate files. An example with seperate files would be very helpful. |
Beta Was this translation helpful? Give feedback.
-
Do you mean you understand everything if it's a single file? Here's the example I created just now from my best understanding from your description. |
Beta Was this translation helpful? Give feedback.
-
I know I'm late to the conversation, but I think I can provide a good example of a use-case similar to what the OP might have been talking about. Imagine that you have an eCommerce store with a product grid and 12 product cards. Each card has a small image carousel, with each image corresponding to a specific color of that product. Each card also has a dropdown to select that product's color. Using Context, I would traditionally wrap each card in its own Provider, keeping the "current color state" synchronized between each card's carousel and dropdown. However, each card has a different state, although they use the same file. This is what I'd like to achieve with zustand, if possible. I'd like to have a single store, used across multiple groups of components at the time time, using only one file, while each card's instance is stored in its own memory space. Is this possible? Thanks |
Beta Was this translation helpful? Give feedback.
-
I have a similar usecase. I created a color picker and handle all states in one store. Now i want to use the same color picker in my single page. For example, i have two options in my page, one to change text color and other to change background. Each of them should have its own instance of that color picker. Any suggestion how i can have create separate instance of that store? |
Beta Was this translation helpful? Give feedback.
-
2 color picker was just an example. Sorry i make a mistake with my words. in one page, i'm using color picker in multiple places. I'm not sure how to use one store for multiple color pickers. Somehow i was able to solve this issue but don't know if it violates any rules. I just have to pass the key to const createColorStore = create<ColorPickerState, [['zustand/immer', never], ['zustand/subscribeWithSelector', never]]>(
immer(
subscribeWithSelector((set, get) => ({
// ...
}))
)
);
type ColorStore = ReturnType<typeof createColorStore>;
const stores: Record<ColorKeyType, ColorStore> = {};
const getStore = (key: ColorKeyType) => {
let store = stores[key];
if (!store) {
store = stores[key] = createColorStore();
}
return store;
};
const useColorStore = (key: ColorKeyType) => {
const ref = useRef(getStore(key));
return ref.current;
};
export default useColorStore; |
Beta Was this translation helpful? Give feedback.
-
I'm confused with What will be the type of selector here? const useColorStore = (key: ColorKeyType, selector: ??? ) => {
return useStore(getStore(key), selector);
}; |
Beta Was this translation helpful? Give feedback.
-
Thanks @dai-shi. One last thing, now if i want to listen for state changes using a |
Beta Was this translation helpful? Give feedback.
-
Hi all
thanks for that great library!
I am using zustand within development of webparts. These webparts can be placed multiple times within the same page.
When I place multiple it multiple times, the both webparts are sharing the same store, but I want them to user their own isolated store.
For the devtools middleware I have seen it is possible to give the name of the store. Can I use that name to dynamically set the store name?
Do you have an example how that could be accomplished?
Thanks alot
Pascal
Beta Was this translation helpful? Give feedback.
All reactions