Move the provider into its own component that renders children.
Acceptance criteria
- 1.#value should show dark.
React Tutorial · Context
These 10 tasks go with the Context Patterns chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Exporting the raw context object makes every consumer repeat the same three lines. Wrapping it in a provider component and a hook is the pattern almost every codebase converges on.” .
1.Wrap the provider in a componentexercise · easy · 10 min
Move the provider into its own component that renders children.
Passes when: #value should show dark.
2.Expose a custom hookexercise · easy · 10 min · practice plan
Wrap useContext in a named hook and use it.
Passes when: #value should show dark.
3.Provide value and setter togetherexercise · medium · 10 min · practice plan
Pass both the value and its setter through one context.
Passes when: #value should show dark.
4.Nest two providersexercise · medium · 10 min · practice plan
Read from two different contexts in one child.
Passes when: #value should show dark Ada.
5.Override a nested providerexercise · easy · 10 min · practice plan
An inner provider wins for the components below it.
Passes when: #value should show inner.
6.Fix the provider that drops its childrenbug fix · easy · 8 min
The wrapper renders its own content instead of children.
Passes when: #value should show dark.
7.Fix the hook reading the wrong contextbug fix · easy · 8 min · practice plan
The custom hook subscribes to the other context.
Passes when: #value should show dark.
8.Fix the setter left out of the valuebug fix · medium · 8 min · practice plan
Only the value is provided, so the child cannot change it.
Passes when: Swapping should show dark.
9.Fix the providers in the wrong orderbug fix · medium · 8 min · practice plan
The child sits outside the second provider.
Passes when: #value should show dark Ada.
10.Fix the inner provider that is ignoredbug fix · easy · 8 min · practice plan
The child is placed under the outer provider only.
Passes when: #value should show inner.
Between them these tasks cover Provider components, Custom hooks and Splitting context.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.