Welcome back! In the previous chapter, Wizard State Management, we built the "Brain" of our application. We learned how the WizardProvider acts like a game console, remembering which level (step) you are on.
But having a Brain is only half the battle. A Brain is useless if the arms and legs (your components) can't talk to it!
The Challenge:
Imagine you are building a "Next Step" button. This button is deep inside your application code. How does this button tell the WizardProvider (which is way up at the top of your app) to flip the page?
We could pass a function down from parent to child, to grandchild, to great-grandchild... like a bucket brigade. But that is messy and tiring.
The Solution:
We use the Context Access Hook: useWizard.
Think of useWizard as a Universal Remote Control. It doesn't matter which room of the house (component) you are standing in. If you have this remote, you can change the channel on the main TV directly.
In React, a "Hook" is just a special function that lets you "hook into" features like state or context.
Our useWizard hook is the bridge. It connects any component, no matter where it is, directly to the central WizardProvider.
Let's solve a specific problem. We want to create a button that, when clicked, moves the Wizard to the next step.
Here is how we do it using our remote control.
First, inside your component, you call the hook. This is like reaching into your pocket and pulling out the remote.
import { useWizard } from './useWizard';
const MyNextButton = () => {
// Grab the "goNext" button from the remote
const { goNext } = useWizard();
// ... rest of component
};
Now that you have the goNext function, you can attach it to a button click.
return (
<button onClick={goNext}>
Go to Next Step
</button>
);
What happens here?
goNext() function fires.WizardProvider. The Provider updates the state index (e.g., from 0 to 1). The entire Wizard re-renders to show the new step!It seems like magic that a button deep down knows about a Provider way up high. Let's peel back the curtain.
When you call useWizard(), React looks upwards through the component tree (like looking up at the ceiling) until it finds the nearest WizardProvider.
Here is the flow of communication:
Let's look at useWizard.ts to see how we built this remote control. It is actually very short!
We need two things from React: useContext (the tool to find the Provider) and the WizardContext object itself (the specific frequency we are listening to).
// Inside useWizard.ts
import { useContext } from 'react'
import { WizardContext } from './WizardProvider.js'
// We also import types to make TypeScript happy
import type { WizardContextValue } from './types.js'
Here is the function we export. It tries to grab the context.
export function useWizard() {
// Ask React: "Give me the data from the nearest WizardProvider"
const context = useContext(WizardContext);
// ... safety check comes next
This is the most important part of the hook.
What happens if you try to use the TV remote when you are miles away in a forest? It won't work because there is no TV nearby.
Similarly, if you use useWizard inside a component that is NOT wrapped by a WizardProvider, context will be null. If we let that slide, your app would crash silently later. instead, we shout an error immediately.
// Did we find a Provider?
if (!context) {
// If not, stop everything and warn the developer!
throw new Error('useWizard must be used within a WizardProvider');
}
// If we found it, hand it over to the component
return context;
}
Why not just export useContext(WizardContext) directly in our components?
useContext AND WizardContext every single time we make a button. useWizard bundles them into one import.In this chapter, we learned:
useWizard hook is like a Universal Remote Control.goNext) or read data without passing props manually.Now we have a Brain (Provider) and a Remote Control (Hook). But what should our Wizard actually look like? How do we make sure every step looks consistent?
In the next chapter, we will build the visual shell of our wizard.
Next Chapter: Standardized Dialog Layout
Generated by Code IQ