I am using ReactJS
and have a form (ponent) that needs to redirect to another ponent, if the post request is successful. I have just started using react router, so this is the way I am trying to redirect.
import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Redirect, Link } from 'react-router-dom';
import NextComponent from '/NextComponent';
import axios from 'axios';
class CurrentComponent extends Component {
constructor() {
super();
this.state = {
value: ''
redirect: false
};
}
handleSubmit() {
axios.post('xxx/yyy', {
xxx: yyy
})
.then(function() {
console.log('Success');
this.setState({redirect: true});
})
.catch(function() {
console.log('Error');
});
}
render() {
return(
<div>
<form>
<div className="form-group">
<input type="name" placeholder="name" />
</div>
<div className="form-group">
<button type="button" onClick={this.handleSubmit.bind(this)} >Submit</button>
{this.state.redirect &&
<Redirect to={{
pathname: '/nextponent',
state: {from: this.state.value}
}} />
}
</div>
<Router>
<Route path="/nextponent" ponent={NextComponent} />
</Router>
</form>
</div>
);
}
}
export default PresentComponent;
It is not redirecting and I have been trying to figure this out. I am sure there are better solutions available, but due to my lack of knowledge I am unsure of implementing it. Any help is appreciated. Thank you.
I am using ReactJS
and have a form (ponent) that needs to redirect to another ponent, if the post request is successful. I have just started using react router, so this is the way I am trying to redirect.
import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Redirect, Link } from 'react-router-dom';
import NextComponent from '/NextComponent';
import axios from 'axios';
class CurrentComponent extends Component {
constructor() {
super();
this.state = {
value: ''
redirect: false
};
}
handleSubmit() {
axios.post('xxx/yyy', {
xxx: yyy
})
.then(function() {
console.log('Success');
this.setState({redirect: true});
})
.catch(function() {
console.log('Error');
});
}
render() {
return(
<div>
<form>
<div className="form-group">
<input type="name" placeholder="name" />
</div>
<div className="form-group">
<button type="button" onClick={this.handleSubmit.bind(this)} >Submit</button>
{this.state.redirect &&
<Redirect to={{
pathname: '/nextponent',
state: {from: this.state.value}
}} />
}
</div>
<Router>
<Route path="/nextponent" ponent={NextComponent} />
</Router>
</form>
</div>
);
}
}
export default PresentComponent;
It is not redirecting and I have been trying to figure this out. I am sure there are better solutions available, but due to my lack of knowledge I am unsure of implementing it. Any help is appreciated. Thank you.
Share Improve this question edited May 23, 2017 at 11:59 Arpit Aggarwal 29.3k16 gold badges96 silver badges110 bronze badges asked May 22, 2017 at 16:52 AnnieAnnie 232 silver badges4 bronze badges 2- I think the issue is that you are adding a state prop to the redirect, but based on the docs the redirect does not have a state prop. reacttraining./react-router/web/api/Redirect – Chaim Friedman Commented May 22, 2017 at 17:10
- Yes thanks, I have changed most of the logic based of it, but because the examples are so concise. I am finding it difficult. – Annie Commented May 23, 2017 at 11:22
6 Answers
Reset to default 1Probably you are not getting the state
after post call, try modifying the handleSubmit
method as:
handleSubmit() {
let _this = this;
axios.post('xxx/yyy', {
xxx: yyy
})
.then(function() {
console.log('Success');
_this.setState({redirect: true});
})
.catch(function() {
console.log('Error');
});
}
Update as per new code:
class ComponentOne extends Component {
constructor() {
super();
this.UpdateRoute= this.UpdateRoute.bind(this);
this.state = {
value: ''
};
this.loggedin = false;
}
const UpdateRoute = () => (
<Router>
<Route exact path="/xyz" render={() => (
loggedin ? (
<ComponentTwo />
) : (
<ComponentOne />
)
)}/>
</Router>
)
handleSubmit() {
let _this = this;
axios.post('/xyz', {
xxx: yyy,
})
.then(function(response) {
_this.loggedin = true;
_this.UpdateRoute();
})
.catch(function() {
console.log('Error');
});
}
render() {
return(
<div>
<h1>Load First</h1>
<button type="button" onClick={this.handleSubmit.bind(this)}>Submit</button>
</div>
);
}
}
export default ComponentOne;
This worked for my use case not sure about you. Try this -
<Route exact path="/some_url" render={() => (
submitted ? (
<Redirect to="/profile"/>
) : (
<HomePage/>
)
)}/>
Modify your logic or encapsulate it into <Route />
like in this example.
If you're using react-router 4, history is available through context:
this.context.history.push('/some/Path');
So you'd modify your handleSubmit:
handleSubmit() {
axios.post('xxx/yyy', {
xxx: yyy
})
.then(function() {
console.log('Success');
this.context.history.push('/go/somewhere/else');
})
.catch(function() {
console.log('Error');
});
}
See this answer for other ways of altering history without conditional routing.
If you are using React-router-dom, then please check below given link.
https://reacttraining./react-router/web/example/auth-workflow
Redirect
is an export of react-router
not react-router-dom
. Therefore, you need to import it like:
import { Redirect } from 'react-router'
Redirect doc
Using react-router 2.8
I used the hashHistory from react-router to achieve this
import { hashHistory } from 'react-router';
then where I want to redirect:
hashHistory.push('/go/somewhere/else');