Skip to content

Commit ba9081f

Browse files
memoize the function and dont run if the object reference does not change
Co-authored-by: Lukas Reining <[email protected]> Signed-off-by: Will C. <[email protected]>
1 parent f438017 commit ba9081f

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

packages/react/src/provider/use-context-mutator.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ import { Context } from './context';
88
*
99
*/
1010
export function useContextMutator() {
11-
const { domain } = useContext(Context) || {};
11+
const { domain } = useContext(Context) || {};
12+
const previousContext = useRef(null);
1213

13-
async function mutateContext(updatedContext: EvaluationContext): Promise<void> {
14-
if (!domain) {
15-
// Set the global context
16-
OpenFeature.setContext(updatedContext);
17-
return;
18-
}
14+
const mutateContext = useCallback(async (updatedContext: EvaluationContext) => {
15+
if (previousContext.current !== updatedContext) {
16+
if (!domain) {
17+
OpenFeature.setContext(updatedContext);
18+
} else {
19+
OpenFeature.setContext(domain, updatedContext);
20+
}
21+
previousContext.current = updatedContext;
22+
}
23+
}, [domain]);
1924

20-
OpenFeature.setContext(domain, updatedContext);
21-
}
22-
23-
return {
24-
mutateContext,
25-
};
25+
return {
26+
mutateContext,
27+
};
2628
}

0 commit comments

Comments
 (0)