Skip to content

Commit

Permalink
fix: #1357 memoize context value in auth-provider
Browse files Browse the repository at this point in the history
  • Loading branch information
pamapa committed Sep 16, 2024
1 parent a549e1e commit 167b9cb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,16 @@ export const AuthProvider = (props: AuthProviderProps): JSX.Element => {
onRemoveUser && await onRemoveUser();
}, [userManager, onRemoveUser]);

const contextValue = React.useMemo(() => {
return {
...state,
...userManagerContext,
removeUser,
};
}, [state, userManagerContext, removeUser]);

return (
<AuthContext.Provider
value={{
...state,
...userManagerContext,
removeUser,
}}
>
<AuthContext.Provider value={contextValue}>
{children}
</AuthContext.Provider>
);
Expand Down
18 changes: 18 additions & 0 deletions test/AuthProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,22 @@ describe("AuthProvider", () => {

mockSigninPopup.mockRestore();
});

it("should not update context value after rerender without state changes", async () => {
// arrange
const wrapper = createWrapper({ ...settingsStub });
const { result, rerender } = await act(async () => {
const { result, rerender } = renderHook(() => useAuth(), {
wrapper,
});
return { result, rerender };
});
const memoized = result.current;

// act
rerender();

// assert
expect(result.current).toBe(memoized);
});
});

0 comments on commit 167b9cb

Please sign in to comment.