Created
October 19, 2021 13:54
-
-
Save cgadski/0f45875024974ba0130f65286a328651 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { useState } from 'react' | |
| function Child(props: { close: () => void }): JSX.Element { | |
| return ( | |
| <button onClick={props.close}>click me to close</button> | |
| ) | |
| } | |
| export default function Parent(): JSX.Element { | |
| // this doesn't work | |
| let [state, setState] = useState(false) | |
| let open = () => { setState(true) } | |
| let close = () => { setState(false) } | |
| let expanded = () => state | |
| // but replacing the definition of close with | |
| // let close = () => { window.setTimeout(() => setState(false), 0) } | |
| // fixes it | |
| return ( | |
| <a onClick={open}> | |
| <p>click me to open</p> | |
| {expanded() && <Child close={close} />} | |
| </a> | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment