If I do this, passing prop as default state, it won’t update if I do
const MyComp = ({ isOpen: _isOpen }) => {
const [isOpen, setIsOpen] = useState(_isOpen)
return <div>{isOpen.toString()}<button onClick={() => setIsOpen(true)}></button></div>
}
I fixed it with this
const MyComp = ({ isOpen: _isOpen }) => {
const [isOpen, setIsOpen] = useState(false)
useEffect(() => {
setIsOpen(_isOpen)
}, [_isOpen])
return <div>{isOpen.toString()}<button onClick={() => setIsOpen(true)}></button></div>
}
is this an anti-pattern?