Prova12
Prova12

Reputation: 653

My handler does not set the state - Material UI

I am using the component "Dialog" from Material UI.

When I click on the button to close the Dialog (which triggers handleCLose), it does not set the state to false. I read a similar problem here: OnClick Listeners not working after closing full-screen dialog using react-material-ui but I have not managed to save it so far.

const Transition = React.forwardRef(function Transition(props, ref) {
    return <Slide direction="up" ref={ref} {...props} />;
});


function Information() {

    const [open, setOpen] = React.useState(false);
    const handleClickOpen = () => {
        setOpen(true);
    };


    const handleClose = () => {
        setOpen(false);
        console.log(open)
    };

    return (
        <div onClick={()=>handleClickOpen()}>
            Information
            <Dialog
                open={open}
                TransitionComponent={Transition}
                keepMounted
                onClose={handleClose}
                aria-labelledby="alert-dialog-slide-title"
                aria-describedby="alert-dialog-slide-description"
            >
                <DialogTitle id="alert-dialog-slide-title">{"Information"}</DialogTitle>
                <DialogContent>
                    <DialogContentText id="alert-dialog-slide-description">
                      Blablabla
                    </DialogContentText>
                </DialogContent>
                <DialogActions>
                    <Button onClick={handleClose} color="primary"> // The function is here
                        Close
                    </Button>
                </DialogActions>
            </Dialog>
        </div>
    );
}

export default Information

Upvotes: 0

Views: 493

Answers (1)

Brian Thompson
Brian Thompson

Reputation: 14355

It looks like you have wrapped your dialog in a div with an onClick function that sets the dialog to open. What is happening is that onClick is being called whenever you click inside the dialog. So even when you click the close button it is calling handleClickOpen. Try separating them:

    return (
        <>  // Add wrapping fragment
            <div onClick={()=>handleClickOpen()}>
                Information
            </div> // close div here
            <Dialog
                open={open}
                TransitionComponent={Transition}
                keepMounted
                onClose={handleClose}
                aria-labelledby="alert-dialog-slide-title"
                aria-describedby="alert-dialog-slide-description"
            >
                <DialogTitle id="alert-dialog-slide-title">{"Information"}</DialogTitle>
                <DialogContent>
                    <DialogContentText id="alert-dialog-slide-description">
                      Blablabla
                    </DialogContentText>
                </DialogContent>
                <DialogActions>
                    <Button onClick={handleClose} color="primary">
                        Close
                    </Button>
                </DialogActions>
            </Dialog>
        </>
    );
}

Upvotes: 2

Related Questions