Skip to content

Instantly share code, notes, and snippets.

@cgadski
Created October 19, 2021 13:57
Show Gist options
  • Select an option

  • Save cgadski/c1648f356d633964e70304765ba7c72a to your computer and use it in GitHub Desktop.

Select an option

Save cgadski/c1648f356d633964e70304765ba7c72a to your computer and use it in GitHub Desktop.
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