Last active
April 6, 2020 13:36
-
-
Save yowchun93/0d1b38c92d33a908c55e5151e6d126d1 to your computer and use it in GitHub Desktop.
Children render props
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function App() { | |
return ( | |
<div className="App"> | |
<Toggle | |
render={(on, toggle) => ( | |
<div> | |
{on && <h1>Hello</h1>} | |
<button onClick={() => toggle()}>toggle</button> | |
</div> | |
)} | |
/> | |
</div> | |
); | |
} | |
const Toggle = ({ render }) => { | |
const [on, setToggle] = React.useState(true); | |
return ( | |
<div> | |
{render({ | |
on: on, | |
toggle: setToggle | |
})} | |
</div> | |
); | |
}; | |
const ToggleRPC = ({ children }) => { | |
const [on, setToggle] = React.useState(true); | |
return children({ | |
on: on, | |
toggle: setToggle | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment