Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

358
Views
¿Cómo puedo hacer un componente reutilizable para Material-UI Snackbar?

Tengo este componente de alerta para usar solo para tener un mensaje que dice "Enviado con éxito" y estoy tratando de usar thin en un componente principal. Sin embargo, no se muestra nada en el componente principal.

AlertComponent

 import React, { useState } from "react"; import { Snackbar, Alert } from "@mui/material"; const AlertComponent = () => { const [open, setOpen] = useState(false); const handleClick = () => { setOpen(true); }; const handleClose = (event, reason) => { if (reason === "clickaway") { return; } setOpen(false); }; return ( <div> {" "} <Snackbar open={open} autoHideDuration={6000} onClose={handleClose}> <Alert onClose={handleClose} severity="success"> Successfully Submitted! </Alert> </Snackbar> </div> ); }; export default AlertComponent;

Componente principal

 const ParentComponent = () => { const [open, setOpen] = useState(false); const onSubmit = async (data) => { //codes to submit the data setOpen(true); //trigger the alert component }; return ( <div> //form here to submit <AlertComponent open={open} /> </div> ); }; export default ParentComponent;

¿Cómo puedo arreglar esto? Gracias.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Aunque @Ghader Salehi ya comentó la solución, pero si alguien no está seguro de cómo controlar la alerta de los padres, aquí está el código.

AlertComponent.js

 import React from "react"; import { Snackbar, Alert } from "@mui/material"; function AlertComponenet(props) { const { open, handleClose } = props; return ( <div> {" "} <Snackbar open={open} autoHideDuration={6000} onClose={handleClose}> <Alert onClose={handleClose} severity="success"> Successfully Submitted! </Alert> </Snackbar> </div> ); } export default AlertComponenet;

Componente principal (en mi código-sandbox utilicé App.js)

 import React, { useState } from "react"; import "./styles.css"; import AlertComponent from "./AlertComponent"; export default function App() { const [open, setOpen] = useState(false); const handleClick = () => { setOpen(true); }; const handleClose = (event, reason) => { if (reason === "clickaway") { return; } setOpen(false); }; return ( <div className="App"> <h1>Hello CodeSandbox</h1> <h2>Start editing to see some magic happen!</h2> <button onClick={handleClick}>Show Alert</button> <AlertComponent open={open} handleClose={handleClose} /> </div> ); }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!