-
Notifications
You must be signed in to change notification settings - Fork 31
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
Use Context to serve the edition
prop
#801
Conversation
PRbuilds results: 💚 AMP validation LightHouse Reporting --automated message |
Have added a few |
I'm keen on this but had a question in Chat:
|
👍 for a great description, very clear! |
Moving the my comments on chat to this PR: The downsides to context are it means you have this airy, magical value that you have to know about. Prop drilling is explicit to it's clear where the data is. But 🤷 sometime prop drillings gets a bit nuts and can be a real pain and context fixes that - so long as you use it carefully, and don't go crazy and put everything in it! The advantage is simplification, and this benefit only grows as we scale. Prop drilling really starts to become an evil, mind numbing nightmare once you begin breaking components out to handle additional complexity. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not a big fan of this for a couple of reasons:
- it is as people have mentioned implicit ('magical' as it's sometimes called) which can seriously harm readabiliity even if it helps concision
- it is introducing effectively global state
- it is coupling us more tightly to React at a time when we are not sure we even want to use it
- it breaks component re-usability/composability (related to the fact it is essentially a global)
But we can discuss IRL - happy to discuss, especially if most people think it is a good idea and disagree with the above!
Closed for now. This is something we could revisit later if we feel the component tree has grown in complexity enough to justify it. |
What does this change?
Adds an
EditionContext
provider that removes the need to keep passingedition
around through multiple levels and replaces it with the Provider / Consumer model so it can be accessed as and when required.Why?
Note on Hooks
React Hooks were added in 16.8 and include the
useContext
hook which means the syntax for using context is now very clean and readable. There's no longer any need to wrap components in<Providers>
and instead you can just useconst edition = useContext(EditionContext);
❤️How to use
If your component wants to access the
edition
then you need 2 lines of code:and then, in your function:
Hooks allow classes to be replaced by functional components so ☝️ should always be sufficient but if you didn't want to migrate to hooks and needed to access
edition
from a class you can use the, still perfectly good, render props pattern:How to test context
If your test depends on the
edition
value then you can provide it to your test html stack in the same way as it's given to the actual app:Link to supporting Trello card
https://trello.com/c/2C74DiyP/744-spike-react-context