How do I stop via a button in the "Menu.jsx" component, call a Dialog within "Register.jsx"

Asked

Viewed 30 times

-1

Personal talk,

I set up a page that has a menu (inside the file "Menu.jsx"), in this menu has a button, I want to open a "dialog", like a pop-up to register notes. This dialog has its own file "Cadastro.jsx"

Menu.jsx

...
export default function ButtonAppBar() {
  const classes = useStyles();

  return (
    <div className={classes.root}>
      <AppBar position="static">
        <Toolbar>
          <Button variant="contained"
            style={{ marginRight: 50 }}
            **onClick={() => ???????}**
          >Cadastrar Notas</Button>
          
          <Typography variant="h5" className={classes.title}>
            Calculadora Darf IR Trader
          </Typography>
          <Button color="inherit">Ajuda</Button>
        </Toolbar>
      </AppBar>
    </div>
  );
}

Cadastro.jsx

export default function FormDialog() {
  const [open, setOpen] = React.useState(false);

  const handleClickOpen = () => {
    setOpen(true);
  };

  const handleClose = () => {
    setOpen(false);
  };

  return (
    <div>
      <Dialog open={open} onClose={handleClose} aria-labelledby="form-dialog-title">
        <DialogTitle id="form-dialog-title">Cadastro de Notas</DialogTitle>
        <DialogContent>
          <DialogContentText>
...

1 answer

-1

The easy solution I see without looking at the rest of the code is that you externalize the state that changes the Dialog view, ie if both components you showed are within the page component Menu, you could:

  1. Put the const [open, setOpen] = React.useState(false);, and the functions handleClickOpen, handleClose in the parent component of both, (which should be the Menu).
  2. Pass off as props the function handleClickOpen for the component ButtonAppBar, and use this in the place you placed the questions (?)
  3. Pass off as props the value of open and handleClose for the component FormDialog
  • Thank you Lucas, I did as you explained and the "onClick" didn’t work, but I confess that I did "in a hurry" so it might have passed something that was left out. I am here with a tabs to read about "useState" and props, I will study better and see where I went wrong. As soon as I get put here as I did.

  • Strange, it should work, I noticed that in these examples you didn’t use any props, and I don’t know how much you already know the syntax, but the props are always objects! So it should be +- so the signature of the functions: Buttonappbar({ handleClickOpen }) But the idea is this, if sibling components need to talk to each other, you should use the parent component as "postman" in this communication.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.