Created
October 19, 2021 13:57
-
-
Save cgadski/c1648f356d633964e70304765ba7c72a 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) | |
| // but it does if you replace the definition of close with | |
| let close = () => window.setTimeout(() => setState(false), 0) | |
| return ( | |
| <a onClick={open}> | |
| <p>click me to open</p> | |
| {state && <Child close={close} />} | |
| </a> | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment