Ideas to update values inside a store with NextJS's revalidatePath in servers actions #3048
-
I'm building a Cart page in a random project that calls from the page level Inside this server page, I wrap my components in a client component that is a wrapper for a context created with React, to share these received props across all child components through a compound pattern, to avoid prop drilling in my case. But, when I try with Zustand's So, my question is: What will be a good approach to use to combine this data update with my current created store? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
If I understand correctly, it sounds like you are creating a store in a context and want that store to update when some data changes. I always do something like this in my provider: type Data = {
foo: string;
};
const StoreContext = createContext<StoreApi<Data> | null>(null);
function createDataStore(value: Data) {
return createStore<Data>((set) => ({
...value,
}));
}
function DataStoreProvider({ children, value }: { children: React.ReactNode; value: Data }) {
const [store] = useState(() => createDataStore(value));
//if any props change, it will update the store
useEffect(() => {
store.setState(value);
}, [value]);
return <StoreContext.Provider value={store}>{children}</StoreContext.Provider>;
}
function useDataStore(selector: (store: Data) => any) {
const store = useContext(StoreContext) as StoreApi<Data> | null;
if (!store) {
throw new Error('useDataStore must be used within a StoreProvider');
}
return useStore(store, selector);
} |
Beta Was this translation helpful? Give feedback.
If I understand correctly, it sounds like you are creating a store in a context and want that store to update when some data changes.
I always do something like this in my provider: