javascript - How to carry snackbar over to next page - Stack Overflow

I'm working on a form and I want to redirect to the home page upon submission.I also want a snac

I'm working on a form and I want to redirect to the home page upon submission. I also want a snackbar to appear upon submission, however, the snackbar will only appear on my form page. When it gets redirected, the snackbar disappears. Is there a way I can have the snackbar remain onscreen even after it redirects?

export default function Form() {
    const {register, handleSubmit} = useForm();
    const [open, setOpen] = useState(false);
    const history = useHistory();
    const handleClose = () => {
      setOpen(false);
    }
    const redir = (data) => {
        console.log(data);
        history.push('/')
        setOpen(true);
        console.log(open);
        }  
   return (
        <div>
            <Navbar />
            <form className="form" onSubmit={handleSubmit(redir)}>
                <Snackbar open={open} autoHideDuration={6000} onClose={handleClose}>
                    <Alert severity='success' onClose={handleClose}>
                        Success! Your message has been sent.
                    </Alert>
                </Snackbar>

I'm working on a form and I want to redirect to the home page upon submission. I also want a snackbar to appear upon submission, however, the snackbar will only appear on my form page. When it gets redirected, the snackbar disappears. Is there a way I can have the snackbar remain onscreen even after it redirects?

export default function Form() {
    const {register, handleSubmit} = useForm();
    const [open, setOpen] = useState(false);
    const history = useHistory();
    const handleClose = () => {
      setOpen(false);
    }
    const redir = (data) => {
        console.log(data);
        history.push('/')
        setOpen(true);
        console.log(open);
        }  
   return (
        <div>
            <Navbar />
            <form className="form" onSubmit={handleSubmit(redir)}>
                <Snackbar open={open} autoHideDuration={6000} onClose={handleClose}>
                    <Alert severity='success' onClose={handleClose}>
                        Success! Your message has been sent.
                    </Alert>
                </Snackbar>
Share Improve this question edited Oct 15, 2020 at 19:46 mkrieger1 23.6k7 gold badges64 silver badges82 bronze badges asked Oct 15, 2020 at 19:42 jjfaressjjfaress 471 silver badge4 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You should call Snackbar ponent with [open, setOpen] not in the Form, but in the Parent ponent and pass setOpen to the Form and call it in redir function.

You're using the router, you can pass the props with push.

history.push({
  pathname: '/',
  openSnackbar: true
});

And then in your parent ponent you have a state for it

const [snackbar, setSnackbar] = useState(location.openSnackbar);

You might need to add a ternary in the useState just in case.

Firstly, I'd use implement the snackbar using HOC or context to make it easier to use anywhere.

Now the solution that works for me: I use it when the user logs in and navigates into the home page.

In the login handler, I did something like this:

if (login === true) {
  localStorage.setItem("isLoggedIn", "true")
//the rest of your implementation
}

In the home page, I did:

useEffect(() => {
    const isLoggedIn = localStorage.getItem("isLoggedIn");
    if (isLoggedIn === "true") {
      // Show Snackbar
      snackbar("Wele user!");

      // Reset the flag in localStorage
      localStorage.removeItem("isLoggedIn");
    }
  }, []);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745218403a4617132.html

相关推荐

  • javascript - How to carry snackbar over to next page - Stack Overflow

    I'm working on a form and I want to redirect to the home page upon submission.I also want a snac

    7小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信