// src/App.jsx
import { useState } from "react";
function Toggle({ children }) {
const [on, setOn] = useState(false);
const toggle = () => setOn((v) => !v);
return children({ on, toggle });
}
function ToggleButton() {
return (
{({ on, toggle }) => (
)}
);
}
function ToggleCheckbox() {
return (
{({ on, toggle }) => (
)}
);
}
function App() {
return (
);
}
export default App;
/*
Notes:
- Both ToggleButton and ToggleCheckbox render a completely different
UI, but both reuse the exact same Toggle component and its on/toggle
logic — neither duplicates the useState call or the toggle function.
- children is called as a function (children({ on, toggle })) rather
than rendered directly as JSX, which is what makes this a render
props pattern rather than ordinary composition.
*/