Instructor: Now we have this toggle component, and we have a couple of built-in components that we can use in tandem with the toggle component to render things where we want and when we want.
What if we wanted to render something else entirely, like instead of the built-in toggle button we have our custom my-toggle button? This isn't going to work at all because it's expecting things to be passed in as props, and it's actually being communicated through context.
Something that we could do is we could use the context types, just like we do with the built-in component, to wire this up. If we're going to be exposing the toggle component for other people to use, we don't want to have to force them to use context themselves. We'd like to abstract that API away a little bit.
Especially since the context API is actually an experimental API in React, it would be nice to shield the user from that API in case there are any unexpected changes that come to the context API in the future.
What we can do to abstract away the context API is we can actually take this toggle button function and come back down here. We're going to make a factory for components that are wired up with the toggle context. We're going to call this withToggle. It's going to take a component.
Here we'll paste in the toggle button and change this to wrapper. Then we're going to return the wrapper. Instead of destructuring on and toggle here, we'll just call this toggle context. Instead of rendering the switch, we'll render whatever component people are passing to us.
We'll forward along all the toggle context and all the props. Then we can take this withToggle function and put it around the my-toggle component. If I save that, everything works. The cool thing is that I can actually bring back the toggle button, and they're referencing the exact same state. I can update one toggle, and it updates the other.
This pattern is called a higher-order component. This function takes a component and returns a new component with some enhanced behaviors that renders the component that it's given. We could also pass options here if we needed to. Many higher-order component factories do that.
The other cool thing about this withToggle higher-order component is we can use it in our own internal components that we have to find up here. Let's go ahead and do that. We'll refactor this a little bit.
For each one of these functions, we're going to make them an assignment too with toggle. We'll change these to arrow functions. We can get rid of the context types for each one of those.
Then we wire things up a little bit differently. We don't know about context or care about context anymore for any of these components, so for both of these we're going to get the on state right from our props. We can get rid of the context reference there.
For this one, we'll go ahead and destructure on and toggle, and the rest of the props we'll call props. We can get rid of the context reference there and then save that. Everything is still working just as it was before, except now it's a little bit nicer because we don't have to worry about the context API.
In review, to enhance an existing component, we create a higher-order component. That's a function that creates a component for us with all the enhancements, returns that component, and then it accepts a component which it is responsible for rendering with all the relevant props.
The component that it creates could be a function component, like we have here, or it could be a class component if we wanted to take advantage of the React lifecycle hooks for our enhancements. The component that we pass, which is being enhanced, can be a function component, like we have here, or a class component to take advantage of the lifecycle hooks too.