javascript - 如何将小吃店带到下一页
问题描述
我正在处理一个表单,我想在提交后重定向到主页。我还希望在提交时出现一个小吃栏,但是,小吃栏只会出现在我的表单页面上。当它被重定向时,小吃店就会消失。有没有一种方法可以让快餐栏在重定向后仍保留在屏幕上?
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>
解决方案
您应该使用 [open, setOpen] 调用Snackbar组件,而不是在 Form 中,而是在 Parent 组件中,并将setOpen传递给 Form 并在 redir 函数中调用它。
推荐阅读
- c# - 使用准备好的语句进行 C# SQL 搜索
- java - 使用 Soap-Ui 在一个 wsdl 中导出 SOAP 服务的 wsdl - Eclipse
- excel - 根据行字体颜色设置 Excel 单元格内容
- oracle - SQL*Plus 忽略 colsep 选项
- android-studio - Flutter Doctor 不识别Android Studio 和flutter dart plugins plugins 新版本4.1更新
- python - 通用元和继承
- html - 如何让模态在html和css中重叠div?
- python - driver.close() 不会关闭 selenium 中的当前选项卡
- php - 类将 DateTime 调用扩展到父构造函数并获取 NULL
- python - 我是否正确使用 Qthread?