admin管理员组文章数量:1415664
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
3 Answers
Reset to default 2You 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");
}
}, []);
本文标签: javascriptHow to carry snackbar over to next pageStack Overflow
版权声明:本文标题:javascript - How to carry snackbar over to next page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745218403a2648278.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论