Create Context Flashcards

1
Q

Creating context

A

Context lets components pass information deep down without explicitly passing props.

Call createContext outside any components to create one or more contexts.

createContext returns a context object. Components can read context by passing it to useContext():

By default, the values they receive will be the default values you have specified when creating the contexts. However, by itself this isn’t useful because the default values never change.

Context is useful because you can provide other, dynamic values from your components:

function App() {
  const [theme, setTheme] = useState('dark');
  const [currentUser, setCurrentUser] = useState({ name: 'Taylor' });

// …

return (

);
}

Now the Page component and any components inside it, no matter how deep, will “see” the passed context values. If the passed context values change, React will re-render the components reading the context as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Importing and exporting context from a file

A

Often, components in different files will need access to the same context. This is why it’s common to declare contexts in a separate file. Then you can use the export statement to make context available for other files:

Components declared in other files can then use the import statement to read or provide this context:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Reference

A

createContext(defaultValue)

Call createContext outside of any components to create a context.

Parameters

defaultValue: The value that you want the context to have when there is no matching context provider in the tree above the component that reads context. If you don’t have any meaningful default value, specify null. The default value is meant as a “last resort” fallback. It is static and never changes over time.

Returns

createContext returns a context object.

The context object itself does not hold any information. It represents which context other components can read or provide. Typically, you will use SomeContext.Provider in components above to specify the context value, and call useContext(SomeContext) in components below to read it. The context object has a few properties:

  • SomeContext.Provider lets you provide the context value to components.
  • SomeContext.Consumer is an alternative and rarely used way to read the context value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

SomeContext.Provider

A

Wrap your components into a context provider to specify the value of this context for all components inside:

function App() {
  const [theme, setTheme] = useState('light');
  // ...
  return (

);
}

value: The value that you want to pass to all the components reading this context inside this provider, no matter how deep. The context value can be of any type. A component calling useContext(SomeContext) inside of the provider receives the value of the innermost corresponding context provider above it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

SomeContext.Consumer

A

Before useContext existed, there was an older way to read context:

function Button() {
  // 🟡 Legacy way (not recommended)
  return (
  {theme => (

  )}

);
}

Although this older way still works, but newly written code should read context with useContext() instead:

function Button() {
  // ✅ Recommended way
  const theme = useContext(ThemeContext);
  return ;
}

children: A function. React will call the function you pass with the current context value determined by the same algorithm as useContext() does, and render the result you return from this function. React will also re-run this function and update the UI whenever the context passed from the parent components have changed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Troubleshooting

A

I can’t find a way to change the context value

Code like this specifies the default context value:

const ThemeContext = createContext(‘light’);

This value never changes. React only uses this value as a fallback if it can’t find a matching provider above.

To make context change over time, add state and wrap components in a context provider.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly