简体   繁体   中英

Need to redirect to previous page after submit

I need to redirect to premontessori page after submitting the form. How can i do this?

this is my handleSubmit function:

handleSubmit(event) {
     event.preventDefault();
     <Link to='/premontessori' style={{textDecoration:'none'}} > alert("New Student Created");</Link>
     var data= {
           Fname:this.state.Fname,
           age:this.state.age,
           Lname:this.state.Lname,
           dob:this.state.dob,
           pob:this.state.pob,
           month:this.state.month,
           nation:this.state.nation,
           mothertongue:this.state.mothertongue,
           bloodgroup:this.state.bloodgroup
      };
      axios.post('/create',data)
        .then(function(response){
        console.log(response);
      })
}

您可以使用react router(例如,hashHistory)进行导航。

hashHistory.goBack();

Link is not the correct way for dynamic routing. You need to make use of context.router to navigate to the previous route

If your component name is MyComponent then add the below line outside the component

MyComponent.contextTypes = {
  router: React.PropTypes.object.isRequired
}

And navigate like this.props.context.router.push('routename');

I will recommend you to do this inside the axios success callback and not before it

class MyComponent extends React.Component {
    ..............
    handleSubmit(event) {
           event.preventDefault();
           var self = this;
            var data= {Fname:this.state.Fname,
               age:this.state.age,
               Lname:this.state.Lname,
               dob:this.state.dob,
               pob:this.state.pob,
               month:this.state.month,
               nation:this.state.nation,
               mothertongue:this.state.mothertongue,
               bloodgroup:this.state.bloodgroup
              };
            axios.post('/create',data)
            .then(function(response){
            console.log(response);
             self.context.router.push('/premontessori')
           })
         }
     ..............
}

MyComponent.contextTypes = {
  router: React.PropTypes.object.isRequired
}

Import the kind of history object you are using like hashHistory or browserHistory .

import {hashHistory} from 'react-router';

and use

hashHistory.push('/premontessori');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM