Next.js Discord

Discord Forum

Disabling a component within a component

Unanswered
Mandarin fish posted this in #help-forum
Open in Discord
Mandarin fishOP
Does anyone know how to turn off or stop rendering a component when a button is pressed inside the component?

6 Replies

const [enabled, setEnabled] = useState(true)
...
return (
  ...
  {enabled && <RenderMe />}
  <button onClick={() => setEnabled(false)}>disable</button>
  ...
)
@Ray ts const [enabled, setEnabled] = useState(true) ... return ( ... {enabled && <RenderMe />} <button onClick={() => setEnabled(false)}>disable</button> ... )
Mandarin fishOP
return (
#Unknown Channel
<section></section>
</>
or
return opened ? <section /> : null
btw, you shouldn't use onClick with a section tag
Mandarin fishOP
it's necessary, like when you click behind a block that will be inside this section to turn it off.