Skip to content

Ideas to update values inside a store with NextJS's revalidatePath in servers actions #3048

Answered by mooalot
dudusotero asked this question in Q&A
Discussion options

You must be logged in to vote

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}>{c…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@dudusotero
Comment options

@dudusotero
Comment options

Answer selected by dudusotero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants