@@ -86,7 +86,10 @@ const getRenderKey = (flagsmith: IFlagsmith, flags: string[], traits: string[] =
8686
8787const getExperimentRenderKey = ( flagsmith : IFlagsmith | null , key : string ) : string => {
8888 const flag = flagsmith ?. getAllFlags ( ) ?. [ key ]
89- return `${ flag ?. value } ${ flag ?. enabled } `
89+ const identifier = flagsmith ?. getContext ( ) . identity ?. identifier ?? null
90+ // Identity is part of the key so that switching identity re-renders (and
91+ // re-fires the exposure) even when the resolved value is unchanged.
92+ return `${ identifier } |${ flag ?. value } |${ flag ?. enabled } `
9093}
9194
9295export function useFlagsmithLoading ( ) {
@@ -227,20 +230,20 @@ export function useExperiment(featureName: string): IFlagsmithFeature | null {
227230 } , [ flagsmith , key ] )
228231
229232 const flag = ( flagsmith ?. getAllFlags ( ) ?. [ key ] as IFlagsmithFeature | undefined ) ?? null
233+ const identifier = flagsmith ?. getContext ( ) . identity ?. identifier ?? null
230234
231235 useEffect ( ( ) => {
232236 if ( ! flagsmith ?. eventsEnabled || ! flag ) {
233237 return
234238 }
235- const identifier = flagsmith . getContext ( ) . identity ?. identifier ?? null
236239 const exposureKey = `${ key } :${ identifier } :${ flag . value } `
237240 if ( lastExposureKey . current === exposureKey ) {
238241 return
239242 }
240243 lastExposureKey . current = exposureKey
241244 flagsmith . getExperimentFlag ( featureName )
242245 // eslint-disable-next-line react-hooks/exhaustive-deps
243- } , [ flagsmith , featureName , key , flag ?. value , flag ?. enabled ] )
246+ } , [ flagsmith , featureName , key , identifier , flag ?. value , flag ?. enabled ] )
244247
245248 return flag
246249}
0 commit comments