DEV Community

Discussion on: help, i am stuck

Collapse
 
lexlohr profile image
Alex Lohr

You shouldn't really manipulate the dom this way in react. The way to do this is to have your close buttons have an onClick handler that calls a callback given as a prop from the parent, i.e.

const ParentComponent = () => {
  const [closed, setClosed] = useState(false)
  const close = useCallback(() => setClosed(true), [])
  return <div style={closed ? { display: 'none' } : null}>
    <ChildComponent close={close}/>
  </div>
}

const ChildComponent = ({close}) => (<span onClick={close}>Close</span>)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
cmuralisree profile image
Chittoji Murali Sree Krishna

Thanks you mr.Alex, 🙏
I will change it according to your suggestion,
Thanks a lot