ReactQuestion 03 of 28
What is the purpose of useContext?
Answer out loud or in your head first, then compare.
Answer
It lets you share data with components deep in the tree without passing props by hand through every level.
The problem:
App
↓ user
Layout
↓ user
Page
↓ user
Header
↓ user
UserMenuThat's prop drilling.
Context lets you skip the components in between.
Typical uses:
- theme,
- the logged-in user,
- language,
- permissions,
- configuration.
And a very useful practical example: libraries use the Context API too.
Material UI:
JSX
<ThemeProvider theme={theme}>
<App />
</ThemeProvider>MUI components further down read the theme without props being passed by hand.
React Hook Form:
JSX
<FormProvider {...methods}>
<Form />
</FormProvider>and later:
JavaScript
const methods =
useFormContext();That's a great example in an interview, because it shows Context used in real libraries.
How did it go? Be honest — “Not yet” puts the question on your review list.