React + redux-form-提交后重定向

马修

我正在使用react-router-dom v4。表单提交成功后,如何重定向到页面?

我遵循了该教程LINK然后,我创建了自己的Submit函数

    const submit=({email='',password=''})=>{
  let error={};
  let isError=false;
  if(email.trim()===''){
    error.email='Required';
    isError=true;
  }
  else if(![ '[email protected]' ].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>

谢谢你的帮助。

Mrinalmech

创建一个状态变量redirectToNewPage,当为true时将进行重定向。callback集合中redirectToNewPage等于true。

class LoginForm extends React.Component {
  state = {
    redirectToNewPage: false
  }

  const submit=({email='',password=''})=>{
     ...
     else{

     //redirect to new page
     this.setState({ redirectToNewPage: true })
     }
   }

   render() {

   // The part that makes the redirect happen
   if (this.state.redirectToNewPage) {
     return (
     <Redirect to="/pageAfterSubmit"/>
     )
   }

   return (
     ...
   )  

  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章