React + redux-form - redirect after submit

I am using response-router-dom v4. How can I redirect to the page after the form is successfully submitted?

I followed this LINK tutorial . Then I created my own 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
    }
}

There should be a funtion redirect in the comment place, but I have no idea how to do this. I tried to put there: <Redirect to="/pageAfterSubmit" push />but nothing happened.

My browser router in index.js is as follows:

<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>

Thanks for any help.

+6
source share
1 answer

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 (
     ...
   )  

  }
}
+5

Source: https://habr.com/ru/post/1016889/


All Articles