Skip to content

Commit

Permalink
no .Provider
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcdodds committed May 13, 2024
1 parent 6f5ede7 commit a60cd36
Show file tree
Hide file tree
Showing 18 changed files with 25 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ controlling whether it's shown or not.

Your job will be to make a `ToggleContext` which will be used to implicitly
share the state between these components. The `Toggle` component will render the
`ToggleContext.Provider` and the other compound components will access that
`ToggleContext` and the other compound components will access that
implicit state via `use(ToggleContext)`.

🦺 TypeScript might not like your `use` call depending on how you set up your
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function Toggle({ children }: { children: React.ReactNode }) {
const [on, setOn] = useState(false)
const toggle = () => setOn(!on)

// 💣 remove this and instead return <ToggleContext.Provider> where
// 💣 remove this and instead return <ToggleContext> where
// the value is an object that has `on` and `toggle` on it. Render children
// within the provider.
return <>TODO...</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ export function Toggle({ children }: { children: React.ReactNode }) {
const [on, setOn] = useState(false)
const toggle = () => setOn(!on)

return (
<ToggleContext.Provider value={{ on, toggle }}>
{children}
</ToggleContext.Provider>
)
return <ToggleContext value={{ on, toggle }}>{children}</ToggleContext>
}

export function ToggleOn({ children }: { children: React.ReactNode }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ export function Toggle({ children }: { children: React.ReactNode }) {
const [on, setOn] = useState(false)
const toggle = () => setOn(!on)

return (
<ToggleContext.Provider value={{ on, toggle }}>
{children}
</ToggleContext.Provider>
)
return <ToggleContext value={{ on, toggle }}>{children}</ToggleContext>
}

export function ToggleOn({ children }: { children: React.ReactNode }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ export function Toggle({ children }: { children: React.ReactNode }) {
const [on, setOn] = useState(false)
const toggle = () => setOn(!on)

return (
<ToggleContext.Provider value={{ on, toggle }}>
{children}
</ToggleContext.Provider>
)
return <ToggleContext value={{ on, toggle }}>{children}</ToggleContext>
}

function useToggle() {
Expand Down
2 changes: 1 addition & 1 deletion exercises/04.slots/01.problem.context/text-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ export function TextField({
// 🐨 create a slots object that has props for both label and input slots
// 💰 the label should provide an htmlFor prop and the input should provide an id

// 🐨 wrap this in a SlotContext.Provider with the value set to the slots object
// 🐨 wrap this in a SlotContext with the value set to the slots object
return children
}
6 changes: 1 addition & 5 deletions exercises/04.slots/01.problem.context/toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ export function Toggle({ children }: { children: React.ReactNode }) {
const [on, setOn] = useState(false)
const toggle = () => setOn(!on)

return (
<ToggleContext.Provider value={{ on, toggle }}>
{children}
</ToggleContext.Provider>
)
return <ToggleContext value={{ on, toggle }}>{children}</ToggleContext>
}

function useToggle() {
Expand Down
2 changes: 1 addition & 1 deletion exercises/04.slots/01.solution.context/text-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export function TextField({
input: { id },
}

return <SlotContext.Provider value={slots}>{children}</SlotContext.Provider>
return <SlotContext value={slots}>{children}</SlotContext>
}
6 changes: 1 addition & 5 deletions exercises/04.slots/01.solution.context/toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ export function Toggle({ children }: { children: React.ReactNode }) {
const [on, setOn] = useState(false)
const toggle = () => setOn(!on)

return (
<ToggleContext.Provider value={{ on, toggle }}>
{children}
</ToggleContext.Provider>
)
return <ToggleContext value={{ on, toggle }}>{children}</ToggleContext>
}

function useToggle() {
Expand Down
2 changes: 1 addition & 1 deletion exercises/04.slots/02.problem.generic/text-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export function TextField({
input: { id },
}

return <SlotContext.Provider value={slots}>{children}</SlotContext.Provider>
return <SlotContext value={slots}>{children}</SlotContext>
}
10 changes: 3 additions & 7 deletions exercises/04.slots/02.problem.generic/toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ export function Toggle({ children }: { children: React.ReactNode }) {
// 🐨 create a slots object that has props for a slot called
// "label" with an htmlFor prop

// 🐨 wrap this in SlotContext.Provider and pass the labelProps in the label slot
// 🐨 add the id to the value in the ToggleContext.Provider
return (
<ToggleContext.Provider value={{ on, toggle }}>
{children}
</ToggleContext.Provider>
)
// 🐨 wrap this in SlotContext and pass the labelProps in the label slot
// 🐨 add the id to the value in the ToggleContext
return <ToggleContext value={{ on, toggle }}>{children}</ToggleContext>
}

function useToggle() {
Expand Down
2 changes: 1 addition & 1 deletion exercises/04.slots/02.solution.generic/text-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export function TextField({
input: { id },
}

return <SlotContext.Provider value={slots}>{children}</SlotContext.Provider>
return <SlotContext value={slots}>{children}</SlotContext>
}
8 changes: 3 additions & 5 deletions exercises/04.slots/02.solution.generic/toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ export function Toggle({
const slots = { label: { htmlFor: id } }

return (
<SlotContext.Provider value={slots}>
<ToggleContext.Provider value={{ on, toggle, id }}>
{children}
</ToggleContext.Provider>
</SlotContext.Provider>
<SlotContext value={slots}>
<ToggleContext value={{ on, toggle, id }}>{children}</ToggleContext>
</SlotContext>
)
}

Expand Down
2 changes: 1 addition & 1 deletion exercises/04.slots/03.problem.prop/text-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export function TextField({
input: { id },
}

return <SlotContext.Provider value={slots}>{children}</SlotContext.Provider>
return <SlotContext value={slots}>{children}</SlotContext>
}
8 changes: 3 additions & 5 deletions exercises/04.slots/03.problem.prop/toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ export function Toggle({
}

return (
<SlotContext.Provider value={slots}>
<SlotContext value={slots}>
{/* 🐨 get rid of the ToggleContext here */}
<ToggleContext.Provider value={{ on, toggle, id }}>
{children}
</ToggleContext.Provider>
</SlotContext.Provider>
<ToggleContext value={{ on, toggle, id }}>{children}</ToggleContext>
</SlotContext>
)
}

Expand Down
2 changes: 1 addition & 1 deletion exercises/04.slots/03.solution.prop/text-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export function TextField({
input: { id },
}

return <SlotContext.Provider value={slots}>{children}</SlotContext.Provider>
return <SlotContext value={slots}>{children}</SlotContext>
}
4 changes: 2 additions & 2 deletions exercises/04.slots/03.solution.prop/toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function Toggle({
const switchProps = { id, on, onClick: toggle }

return (
<SlotContext.Provider
<SlotContext
value={{
label: labelProps,
onText: onTextProps,
Expand All @@ -29,6 +29,6 @@ export function Toggle({
}}
>
{children}
</SlotContext.Provider>
</SlotContext>
)
}
2 changes: 1 addition & 1 deletion exercises/04.slots/README.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function NumberField({ children }: { children: React.ReactNode }) {
description: { id: descriptionId },
input: { id: inputId, 'aria-describedby': descriptionId },
}
return <SlotContext.Provider value={slots}>{children}</SlotContext.Provider>
return <SlotContext value={slots}>{children}</SlotContext>
}
```

Expand Down

0 comments on commit a60cd36

Please sign in to comment.