From 798012524fc358fc7ec50a9470c88001d2dd944e Mon Sep 17 00:00:00 2001 From: Criezc Date: Wed, 19 Mar 2025 15:41:45 +0700 Subject: [PATCH 1/2] feat: fix context value reference change across render --- src/FlagProvider.tsx | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/FlagProvider.tsx b/src/FlagProvider.tsx index de05c05..b7793c3 100644 --- a/src/FlagProvider.tsx +++ b/src/FlagProvider.tsx @@ -1,7 +1,7 @@ /** @format */ -import React, { type FC, type PropsWithChildren, useEffect, useMemo, useState } from 'react'; -import { type IConfig, UnleashClient } from 'unleash-proxy-client'; +import React, { type FC, type PropsWithChildren, useCallback, useEffect, useMemo, useState } from 'react'; +import { type IConfig, IMutableContext, UnleashClient } from 'unleash-proxy-client'; import FlagContext, { type IFlagContextValue } from './FlagContext'; export interface IFlagProvider { @@ -106,20 +106,47 @@ const FlagProvider: FC> = ({ }; }, []); + const on = useCallback( + (event: string, callback: Function, ctx?: any) => + client.current.on(event, callback, ctx), + [] + ) as IFlagContextValue['on']; + + const off = useCallback( + (event: string, callback?: Function) => client.current.off(event, callback), + [] + ) as IFlagContextValue['off']; + + const isEnabled = useCallback( + (toggleName: string) => client.current.isEnabled(toggleName), + [] + ) as IFlagContextValue['isEnabled']; + + const updateContext = useCallback( + async (context: IMutableContext) => + await client.current.updateContext(context), + [] + ) as IFlagContextValue['updateContext']; + + const getVariant = useCallback( + (toggleName: string) => client.current.getVariant(toggleName), + [] + ) as IFlagContextValue['getVariant']; + const context = useMemo( () => ({ - on: ((event, callback, ctx) => client.current.on(event, callback, ctx)) as IFlagContextValue['on'], - off: ((event, callback) => client.current.off(event, callback)) as IFlagContextValue['off'], - updateContext: async (context) => await client.current.updateContext(context), - isEnabled: (toggleName) => client.current.isEnabled(toggleName), - getVariant: (toggleName) => client.current.getVariant(toggleName), + on, + off, + updateContext, + isEnabled, + getVariant, client: client.current, flagsReady, flagsError, setFlagsReady, setFlagsError, }), - [flagsReady, flagsError] + [flagsReady, flagsError, on, off, updateContext, isEnabled, getVariant] ); return ( From ad764a3c1c62af22943bf761d4775840c2962270 Mon Sep 17 00:00:00 2001 From: Criezc Date: Tue, 8 Apr 2025 09:55:39 +0700 Subject: [PATCH 2/2] feat: update types --- src/FlagProvider.tsx | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/FlagProvider.tsx b/src/FlagProvider.tsx index b7793c3..2943ecc 100644 --- a/src/FlagProvider.tsx +++ b/src/FlagProvider.tsx @@ -106,32 +106,25 @@ const FlagProvider: FC> = ({ }; }, []); - const on = useCallback( - (event: string, callback: Function, ctx?: any) => - client.current.on(event, callback, ctx), - [] - ) as IFlagContextValue['on']; + const on = useCallback(client.current.on, []); - const off = useCallback( - (event: string, callback?: Function) => client.current.off(event, callback), - [] - ) as IFlagContextValue['off']; + const off = useCallback(client.current.off, []); - const isEnabled = useCallback( + const isEnabled = useCallback( (toggleName: string) => client.current.isEnabled(toggleName), [] - ) as IFlagContextValue['isEnabled']; + ) - const updateContext = useCallback( + const updateContext = useCallback( async (context: IMutableContext) => await client.current.updateContext(context), [] - ) as IFlagContextValue['updateContext']; + ) - const getVariant = useCallback( + const getVariant = useCallback( (toggleName: string) => client.current.getVariant(toggleName), [] - ) as IFlagContextValue['getVariant']; + ) const context = useMemo( () => ({