Skip to content

Commit

Permalink
fix: useData lose reactivity
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 committed Sep 4, 2024
1 parent 92bf1bd commit 3fa1f95
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion vike-solid/hooks/useData.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { createStore } from "solid-js/store";

export { useData };

import { createEffect } from "solid-js";
import { usePageContext } from "./usePageContext.js";

/** Access `pageContext.data` from any SolidJS component
Expand All @@ -9,6 +12,14 @@ import { usePageContext } from "./usePageContext.js";
* - https://vike.dev/pageContext-anywhere
*/
function useData<Data>(): Data {
const { data } = usePageContext() as any;
const ctx = usePageContext() as any;

// sub store to keep reactivity https://github.com/vikejs/vike-solid/issues/114
const [data, setData] = createStore(ctx.data);

createEffect(() => {
setData(ctx.data);
});

return data;
}

0 comments on commit 3fa1f95

Please sign in to comment.