首页 > 解决方案 > 如何将小吃店带到下一页

问题描述

我正在处理一个表单,我想在提交后重定向到主页。我还希望在提交时出现一个小吃栏,但是,小吃栏只会出现在我的表单页面上。当它被重定向时,小吃店就会消失。有没有一种方法可以让快餐栏在重定向后仍保留在屏幕上?

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>

标签: javascriptreactjs

解决方案


您应该使用 [open, setOpen] 调用Snackbar组件,而不是在 Form 中,而是在 Parent 组件中,并将setOpen传递给 Form 并在 redir 函数中调用它。


推荐阅读