Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save cgadski/0f45875024974ba0130f65286a328651 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) }
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