我正在使用react-router-dom v4 . 表单提交成功后如何 redirect 到页面?
我按照那个教程LINK . 然后我创建了自己的 submit function :
const submit=({email='',password=''})=>{
let error={};
let isError=false;
if(email.trim()===''){
error.email='Required';
isError=true;
}
else if(![ 'test@wp.pl' ].includes(email)){
error.email='User does not exist';
isError=true;
}
if(password.trim()===''){
error.password='Required';
isError=true;
}
else if(password!=='test'){
error.password='Wrong password';
isError=true;
}
if(isError){
throw new SubmissionError(error);
}
else{
//redirect to new page
}
}
在评论的地方应该有一个重定向功能,但我不知道如何做到这一点 . 我试过放在那里: <Redirect to="/pageAfterSubmit" push />
但没有发生任何事 .
我在 index.js 的浏览器路由器如下所示:
<BrowserRouter >
<Provider store={store}>
<div>
<Route path="/" component={LoginForm}></Route>
<Route path="/markers" component={Markers}></Route>
<Route path="/contact" component={Contact}></Route>
</div>
</Provider >
</BrowserRouter>
谢谢你的帮助 .
1 回答
创建一个状态变量
redirectToNewPage
,当为true时进行重定向 . 在callback
中设置redirectToNewPage
等于true .