Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memoize context value in auth0-provider #318

Merged
merged 1 commit into from
Jan 13, 2022

Conversation

claycoleman
Copy link
Contributor

@claycoleman claycoleman commented Jan 12, 2022

By submitting a PR to this repository, you agree to the terms within the Auth0 Code of Conduct. Please see the contributing guidelines for how to create and submit a high-quality PR for this repo.

Description

We've seen a few instances where the context value in Auth0Provider needs to be memoized, as it is set to a new object value on each re-render and causes instances where we use the auth0 object in useEffect or useCallback to continuously re-evaluate.

References

Similar to #150, but now we're memoizing the whole object.

Testing

Added a new test to auth0-provider.test and ran without my changes:

 FAIL  __tests__/auth-provider.test.tsx
  ● Auth0Provider › should not update context value after rerender with no state change

    expect(received).toBe(expected) // Object.is equality

    If it should pass with deep equality, replace "toBe" with "toStrictEqual"

    Expected: {"buildAuthorizeUrl": [Function anonymous], "buildLogoutUrl": [Function anonymous], "error": undefined, "getAccessTokenSilently": [Function anonymous], "getAccessTokenWithPopup": [Function anonymous], "getIdTokenClaims": [Function anonymous], "handleRedirectCallback": [Function anonymous], "isAuthenticated": true, "isLoading": false, "loginWithPopup": [Function anonymous], "loginWithRedirect": [Function anonymous], "logout": [Function anonymous], "user": {"name": "foo", "updated_at": "1"}}
    Received: serializes to the same string

      828 |     rerender();
      829 | 
    > 830 |     expect(result.current).toBe(memoized);
          |                            ^
      831 |   });
      832 | });
      833 | 

      at __tests__/auth-provider.test.tsx:830:28
      at fulfilled (__tests__/auth-provider.test.tsx:5:58)

then ran again with my changes:

 PASS  __tests__/auth-provider.test.tsx
  • This change adds test coverage for new/changed/fixed functionality

Checklist

  • I have added documentation for new/changed functionality in this PR or in auth0.com/docs
  • All active GitHub checks for tests, formatting, and security are passing
  • The correct base branch is being used, if not master

@claycoleman claycoleman requested a review from a team as a code owner January 12, 2022 15:25
@adamjmcgrath adamjmcgrath merged commit f8ee6c5 into auth0:master Jan 13, 2022
@adamjmcgrath
Copy link
Contributor

Thanks @claycoleman!

@claycoleman
Copy link
Contributor Author

awesome, thanks @adamjmcgrath! any updates on when the next release will be? looks like the library hasn't been released since September

@adamjmcgrath adamjmcgrath mentioned this pull request Jan 14, 2022
@adamjmcgrath
Copy link
Contributor

@claycoleman This has been release in 1.9.0

@claycoleman claycoleman deleted the memoize-auth0-provider-value branch February 3, 2023 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants