Skip to content

Commit

Permalink
Merge pull request #1361 from authts/fix-1357
Browse files Browse the repository at this point in the history
memoize context value in auth-provider
  • Loading branch information
pamapa authored Sep 17, 2024
2 parents 2d20f6a + 167b9cb commit 94fa698
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 94fa698

Please sign in to comment.