I have two seperate components namely Header and Modal. There is a button in Navbar that has an onClick function attached to it. By clicking it the state changes and hence the state is passed to modal as props which then triggers it. The state is then set to false again. However if i click on the button again, the modal does not appear. I have tried many different thing even with useEffects but nothing worked.
Header code
const Header = () => {
const [modal, setModal] = useState(false)
return(
<div>
{modal ? <ModalProduct showModal={true} /> : null}
<ul class="nav navbar-nav ml-auto">
<li><Button variant="danger" style={{ marginTop: '8px' }} onClick={() => setModal(true)}>Add
new product</Button></li></ul>)
</div>)
And for the Modal Component I have
export default function ModalProduct(props) {
const [show, setShow] = useState(props.showModal);
const handleClose = () =>
setShow(false);
return (
< div >
<Modal show={show} onHide={handleClose}>
...
</Modal>
</div >
);
}
There is something related to do with hooks, it gets true for the first time popping the modal and then changes to false but then does not become true again.