Skip to content

Instantly share code, notes, and snippets.

@mzibari
Created May 15, 2020 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzibari/3fea5d32c0a678b32d696d5f29df659c to your computer and use it in GitHub Desktop.
Save mzibari/3fea5d32c0a678b32d696d5f29df659c to your computer and use it in GitHub Desktop.
What situations would be good to use context?
When more than one component in a deeply nested tree need to access some data in our app, conext will be a good choice.
If you need to pass a prop down 1 or 2 levels, is context necessary?
No, it's not necessary. Applying context makes component's reuse more difficult.
Can you pass a component instance as a prop to avoid the need for context?
Yes you can, this is what's called component composition.
Can you write your own components that accept render props?
Yes I can
No seriously
We can use Context.Consumer to do that
const colorContext = React.createContext ({
color: 'blue'
});
export default colorContext;
import colorContext from './......
.
.
.
<colorContext.Consumer>
{function renderProp() {
<div />
}
</colorContext.Consumer>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment