I'm making a modal in hooks that opens and closes with buttons, but nothing happens.
function CartTotal(props){
const [open, setOpen] = useState(false);
function handleOpen(){
if(setOpen === true){
return alert('Abierto')
}
}
function handleClose(){
if(setOpen === false){
return alert('cerrado')
}
}
return(
<div>
<div className="shopCart">
<br/>
<p className="total">Precio Total de Todo: ${totalPrice}</p>
<br/>
<p className="total">Total de Productos: {totalProducts}</p>
<br/>
<button open={open} onClick={handleOpen}>Open</button>
<br/>
<button onClick={handleClose}>Close</button>
</div>
</div>
)
}
export default CartTotal;
The variable that contains
true
orfalse
isopen
setOpen
is the function.You must change:
by
and if you want to change the state to call the function too, it would look like this:
As @AdolfoOnrubia's answer says, it
setOpen
is the function that modifiesopen
, considering this, you can make a single function to open and close: