Skip to content

Commit

Permalink
fix: add instructions for exercice 03.compound-components (#133)
Browse files Browse the repository at this point in the history
* fix: add instructions for exercice 03.compound-components

* Update exercises/03.compound-components/02.problem.validation/toggle.tsx

---------

Co-authored-by: Kent C. Dodds <[email protected]>
  • Loading branch information
flexbox and kentcdodds authored Oct 18, 2024
1 parent e8399be commit 4bdcf46
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ export function Toggle({ children }: { children: React.ReactNode }) {
return <ToggleContext value={{ on, toggle }}>{children}</ToggleContext>
}

// 🐨 create a custom useToggle() hook here
// create a new context variable and read ToggleContext with use
// when no context is found, throw an error with a useful message
// otherwise return the context

export function ToggleOn({ children }: { children: React.ReactNode }) {
// 🐨 instead reading context with use, we'll need to get that from useToggle()
const { on } = use(ToggleContext)!
return <>{on ? children : null}</>
}

export function ToggleOff({ children }: { children: React.ReactNode }) {
// 🐨 instead reading context with use, we'll need to get that from useToggle()
const { on } = use(ToggleContext)!
return <>{on ? null : children}</>
}
Expand All @@ -25,6 +32,7 @@ type ToggleButtonProps = Omit<React.ComponentProps<typeof Switch>, 'on'> & {
on?: boolean
}
export function ToggleButton({ ...props }: ToggleButtonProps) {
// 🐨 instead reading context with use, we'll need to get that from useToggle()
const { on, toggle } = use(ToggleContext)!
return <Switch {...props} on={on} onClick={toggle} />
}

0 comments on commit 4bdcf46

Please sign in to comment.