From a6c1c49f98776bec88bf7a366ca57fec1baf2ae4 Mon Sep 17 00:00:00 2001 From: Zach Pomerantz Date: Tue, 13 Sep 2022 09:03:59 -0700 Subject: [PATCH] fix: update feature flags on update (#4613) --- src/featureFlags/index.tsx | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/featureFlags/index.tsx b/src/featureFlags/index.tsx index 7fc7b75dea..036acddef1 100644 --- a/src/featureFlags/index.tsx +++ b/src/featureFlags/index.tsx @@ -1,5 +1,4 @@ -import { useAtom } from 'jotai' -import { atomWithStorage, useAtomValue } from 'jotai/utils' +import { atomWithStorage, useAtomValue, useUpdateAtom } from 'jotai/utils' import { createContext, ReactNode, useCallback, useContext } from 'react' export { FeatureFlag } from './flags/featureFlags' @@ -23,14 +22,16 @@ export function useFeatureFlagsContext(): FeatureFlagsContextType { export const featureFlagSettings = atomWithStorage>('featureFlags', {}) export function useUpdateFlag() { - const [featureFlags, setFeatureFlags] = useAtom(featureFlagSettings) + const setFeatureFlags = useUpdateAtom(featureFlagSettings) return useCallback( (featureFlag: string, option: string) => { - featureFlags[featureFlag] = option - setFeatureFlags(featureFlags) + setFeatureFlags((featureFlags) => ({ + ...featureFlags, + [featureFlag]: option, + })) }, - [featureFlags, setFeatureFlags] + [setFeatureFlags] ) }