Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add instructions for exercice 03.compound-components #133

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 usefull debugging information
kentcdodds marked this conversation as resolved.
Show resolved Hide resolved
// 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} />
}