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

feat(v2): Error when hooks depends on context is used outside of Layout #2974

Merged
merged 1 commit into from
Jun 23, 2020
Merged

feat(v2): Error when hooks depends on context is used outside of Layout #2974

merged 1 commit into from
Jun 23, 2020

Conversation

SamChou19815
Copy link
Contributor

Motivation

Close #2912 to give user a clear direction.

I didn't create a docs pointer for the useUserPreferencesContext hook since it's more internal and unlike to be used by pages in top level.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work. Bonus points for screenshots and videos!)

Try to add useThemeHook useUserPreferencesContext to website/src/pages/index.js and see it crash.

Related PRs

(If this PR adds or changes functionality, please take some time to update the docs at https://github.com/facebook/docusaurus, and link to your PR here.)

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Jun 20, 2020
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Jun 20, 2020

Deploy preview for docusaurus-2 ready!

Built with commit 289875d

https://deploy-preview-2974--docusaurus-2.netlify.app

@SamChou19815 SamChou19815 marked this pull request as ready for review June 20, 2020 18:27
Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not fan of the using uninitialized string

  • it needs to check all context values on consuming side which is kind of useless

setLightTheme: () => {},
setDarkTheme: () => {},
setLightTheme: 'UNINITIALIZED',
setDarkTheme: 'UNINITIALIZED',
Copy link
Collaborator

@slorber slorber Jun 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not very fan of using fn: 'UNINITIALIZED' this way.

It's more common in React to have a context being initialized to undefined/null by default, and have a hook to read that context doing the null-check.

Can be seen here:
https://kentcdodds.com/blog/how-to-use-react-context-effectively#typescript--flow
https://medium.com/@dai_shi/a-thought-on-react-context-default-value-fb3283cb5788

@slorber slorber merged commit 11b7ce5 into facebook:master Jun 23, 2020
@slorber slorber added the pr: new feature This PR adds a new API or behavior. label Jun 23, 2020
@slorber
Copy link
Collaborator

slorber commented Jun 23, 2020

LGTM 👍

@SamChou19815 SamChou19815 deleted the error-on-context-hooks-out-of-layout branch June 23, 2020 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: new feature This PR adds a new API or behavior.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[bug] isDarkTheme from useThemeContext always returns false
4 participants