From 6539187318fcd883b616f575ff89873e90b3f477 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Mu=C3=B1oz=20Tous?= Date: Mon, 30 Sep 2024 10:02:04 +0200 Subject: [PATCH] fix: referenced zustand causing store not to be properly updated --- src/shared/presentation/utils/zustand.tsx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/shared/presentation/utils/zustand.tsx b/src/shared/presentation/utils/zustand.tsx index 264772e..d649db1 100644 --- a/src/shared/presentation/utils/zustand.tsx +++ b/src/shared/presentation/utils/zustand.tsx @@ -1,6 +1,6 @@ import type { ConstructorType } from "@/src/shared/domain/models/constructor-type"; import type { PropsWithChildren } from "react"; -import { createContext, useContext, useRef } from "react"; +import { createContext, useContext } from "react"; import type { StateCreator, StoreMutatorIdentifier } from "zustand"; import { createStore, useStore } from "zustand"; @@ -46,6 +46,7 @@ export function createProvider( const factoryInitializer = ({ initialState, builderProps }: FactoryInitializerParams) => { if (initialState && builderProps) throw new Error("Both initialState and builderProps can't be used at the same time"); const base = builderMiddleware(builderProps || ({} as P), builderInitializer); + if (initialState) { return immer(merge(initialState, base)); } else { @@ -64,7 +65,8 @@ export function createProvider( const State = ({ children, initialState, builderProps }: PropsWithChildren>) => { const _initialState = initialState as T | undefined; - const store = useRef(storeFactory({ initialState: _initialState, builderProps })).current; + const store = storeFactory({ initialState: _initialState, builderProps }); + return {children}; };