From 3fa1f9561a1fa7f8811e67b6b3fb020cc4223955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20Charles?= Date: Wed, 4 Sep 2024 12:27:03 +0200 Subject: [PATCH] fix: useData lose reactivity --- vike-solid/hooks/useData.tsx | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/vike-solid/hooks/useData.tsx b/vike-solid/hooks/useData.tsx index a9339bf..90a3bac 100644 --- a/vike-solid/hooks/useData.tsx +++ b/vike-solid/hooks/useData.tsx @@ -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 @@ -9,6 +12,14 @@ import { usePageContext } from "./usePageContext.js"; * - https://vike.dev/pageContext-anywhere */ function useData(): 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; }