Slot Prop

Loading "Slot Prop"
๐Ÿ‘จโ€๐Ÿ’ผ We have ToggleOn and ToggleOff components, but really we could make those components a simple Text component that accepts a slot prop. Then the Toggle component could define the props that the individual Text components should have based on which slot they're taking.
In fact, we could do this with the Switch as well!
๐Ÿงโ€โ™‚๏ธ I've added Text and Switch components to the file for you to use. These are both already wired up to consume a slot named text and switch. You can check the diff for details.
What we want to do in this exercise is add a slot prop to each of our slot components so the slot they're taking can be defined by the parent component.
Then you'll need to update Toggle to get rid of the ToggleContext provider and instead use the SlotProvider for all the components it wants to send props to:
  • label - htmlFor
  • onText - hidden (undefined if isOn is true, and true if isOn is false)
  • offText - hidden (undefined if isOn is false, and true if isOn is true)
  • switch - id, on, and onClick
So by the end of all of this, here's what I want the API to be like:
<Toggle>
	<Label>Party mode</Label>
	<Switch />
	<Text slot="onText">Let's party ๐Ÿฅณ</Text>
	<Text slot="offText">Sad town ๐Ÿ˜ญ</Text>
</Toggle>
Once that's been updated, you can delete the useToggle hook and the ToggleOn, ToggleOff, and ToggleButton components.
Reusability FTW!
Login to get access to the exclusive discord channel.
  • General
    Welcome to EpicReact.dev! Say Hello ๐Ÿ‘‹
    Kent C. Dodds โ—† ๐Ÿš€๐Ÿ†๐ŸŒŒ:
    Welcome to the first of many posts in the EpicReact.dev channel! Take a moment to introduce yourself...
    0 ยท 4 days ago