Instructor: here I am using the toggler props collection on the switch and the button. Now let's say that I want to be notified when this button is clicked on. I'm going to add an on click. I'll just have this arrow function that alerts with hi.
Then I click on this. I get the alert. Oh, but the toggle state isn't being updated. Hmm, why is that?
The problem is that we're spreading the toggler props first. Then we spread on click here. On click is going to override the props that I get from toggler props. If I swap these, then I'm going to get the toggle functionality, but I'm not going to get my alert because now the opposite is happening.
I need to actually compose these things together so that both of these functions are called. One thing that I could do is I'll move this on click back down.
Then I'll do my thing. I'll say toggler props.on click. That's going to get the behavior that I'm looking for.
The problem is that perhaps one day on click gets changed to some other event like on key down or something. Then that's going to break this because toggler props will no longer have on click. You could argue that knowing what on click is for this toggler props could be an implementation detail. All that really matters with the toggler props is that it's applied to the button that's going to be doing the toggling.
You don't want users of toggler props to have to understand what the implementation details of toggler props is just to make use of it. That's exactly what we're doing when users have to use toggler props to on click. That's a bit of a leaky abstraction.
Another way I could solve this problem is if the toggle component called the render function with some sort of utility or something that allowed me to compose the props that I want to apply to the button, in such a way that it doesn't expose some implementation details. But it also composes the behavior that I want to apply to the button in addition to whatever it needs to do to do its job.
To do this, we're going to change from toggler props to get toggler props. This is actually going to be a function that you call. In the switch case, we'll just call it like a regular function with no arguments because I don't need it to do anything special. In the button case, I'm going to call it with an object.
This object is going to be all of the props that I want to have applied to the button.
Get toggler props will be responsible for making sure it composes together as I would expect it to. We'll say on click and alert hi. Then we'll get rid of this prop.
Then we're going to move up here. We'll switch toggler props with get toggler props. I'm going to create a new property called this.get toggler props.
Then we'll take this object. We'll move it up to that new method, get toggler props.
This function is going to take our props,and it's going to return these new props. A part of this function's job is to make it so that people using it don't have to understand how it works, so I should be able to call the get toggler props function with any of the props that I have. It should compose those things together just perfectly without me having to know what's going on.
For example, if I wanted to add an ID prop to this get toggler props invocation, it should apply that ID prop to the button. In this case, it's not going to do anything special with it, but the user of this function should only know that they need to pass all of their props to it, and all of the props that they want to have applied to the button will be applied properly even if it means it's composed like in the case of on click.
Let's go ahead and make that happen by adding ...props to spread all of the props that we don't care about across what we're returning here. That way people can add any prop that they like and it'll be forwarded on.
But we do want to handle the on click. We don't want that to be overridden when we spread these props. We are going to need to de-structure this to be on click and then the rest of the props. But sometimes people are going to call this without anything, so we'll default this to an empty object.
Now let's go ahead and compose the on click prop together. I'll make a new function. Here we'll say this.toggle. We'll make sure we take all the args and forward it along. Now that's functionally equivalent to what we had before.
Then we'll call on click with the args, and we should be good to go. Now if I click on this, I'm going to get the alert and I'm going to get the toggle. Perfect.
Then I click on the toggle. Uh-oh, something's going on here. The problem is that I'm calling get toggler props with nothing. I default that to an empty object. On click is going to be undefined. There are a couple of ways we can solve this.
I'm going to go ahead and say on click and on click. If on click is falsey then this side of the double ampersand will never run. Then we'll just call toggle. Now everything is working.
I'm actually going to rewrite this by making a helper function. It's going to be called compose. That's going to equal a function that accepts any number of functions. That's going to return a new function that accepts any number of arguments.
Then we'll say for each of the functions, we'll take that function. If that function exists, we'll call it with args. We essentially rewrote this piece just in a more generic way.
Now I can say compose on click and this.toggle. It works exactly as it had before.
This pattern is called prop getters. It's used in combination with the render prop pattern to make it easier for common-use cases to apply the correct props based off of the state. We use a getter function so we can compose things together without the end user having to understand what exactly is going on under the hood to make the right props apply to the right elements.
To do this, we create a function on our toggle component. We accept all the props that are passed to us, defaulting it to an empty object so people can call it without any arguments. Then we return an object of all the props that we want to have applied, composing any of the event handlers that are necessary to make our job work.
You probably want to do something similar to this with class name prop if you had something you'd want to combine those together in a sensible way, so that users can apply their own class names as well as any class names that you want to have applied.