Skip to content

Instantly share code, notes, and snippets.

@nathanielmata
Last active June 3, 2020 03:15
Show Gist options
  • Save nathanielmata/b53a41754a5096d1dba9ba77918a6fa3 to your computer and use it in GitHub Desktop.
Save nathanielmata/b53a41754a5096d1dba9ba77918a6fa3 to your computer and use it in GitHub Desktop.
Creating and Reading Contex

*Brian *Sultana *Nathaniel

  1. What situations would be good to use context? Context is designed to share data that can be considered “global” for a tree of React components, such as the current authenticated user, theme, or preferred language. Deeply nested components that need global information.

  2. If you need to pass a prop down 1 or 2 levels, is context necessary? No, context is only necessary when working through many levels. If you only want to avoid passing some props through many levels, component composition is often a simpler solution than context. Common examples where using context might be simpler than the alternatives include managing the current locale, theme, or a data cache

  3. Can you pass a component instance as a prop to avoid the need for context? Yes, by setting a component to a variable and passing that variable as a prop you can avoid using context. const content = <Feed user={user} />;

  4. Can you write your own components that accept render props? Yes, any prop that is a function that a component uses to know what to render is technically a “render prop”. There are no limitations on what you can pass as props in React.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment