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

366
Views
How can I make a reusable component for Material-UI Snackbar?

I have this Alert component to be used just to have a message that says "Successfully submitted" and I'm trying to use thin in a parent component. However, nothing shows in the parent component.

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;

Parent Component

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;

How can I fix this? Thank you.

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

0

Although @Ghader Salehi commented already the solution but if anyone is not sure how to control the alert from parent here is the code.

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;

Parent Component (In my code-sandbox I used 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!