Reputation: 2033
So, I try to run the function by condition: if I got an Error in the catch
method.
To do this, I implement the this.state.loginError
in component state, that will change on true
if we got an Error. So, and after error
- the this.state.loginError
comese back with true
(and this is also I saw in console.log), but after state changes - my function loginError(target)
does not want to start anyway.
See my code and logs below, please:
class LoginPage extends Component {
constructor(props) {
super(props);
this.state = {
navigate: false,
loginError: false,
}
}
handleSubmit = (e) => {
e.preventDefault();
axios.post('http://localhost:3016/auth/login', userLogin, {withCredentials: true})
.catch(err => {
this.setState({
loginError: true
});
console.log(this.state.loginError); // gives `true`
});
if (this.state.loginError) {
console.log('Error!') //does not work
loginError(target);
}
};
Upvotes: 1
Views: 68
Reputation: 4464
Because axios.post
returns a promise, all code you'll write after it will be executed before the .then()
or .catch()
statements. If your need is to call loginError()
function when the request fails you can call it in .catch
statement :
axios.post('http://localhost:3016/auth/login', userLogin, {withCredentials: true})
.catch(err => {
loginError(target);
});
If you need your function to be executed after updating the state you can use the setState
callback (second argument) :
axios.post('http://localhost:3016/auth/login', userLogin, {withCredentials: true})
.catch(err => {
this.setState({ loginError: true }, () => { loginError(target); })
});
Upvotes: 0
Reputation: 957
Why don't you try Promises instead, which is very clear and simple way around.
class LoginPage extends Component {
constructor(props) {
super(props);
this.state = {
navigate: false,
loginError: false,
}
}
handleSubmit = (e) => {
e.preventDefault();
return new Promise(resolve, reject){
axios.post('http://localhost:3016/auth/login', userLogin, {withCredentials: true})
.catch(err => {
reject(err);
})
.then(result => resolve(result));
}
//Else where in Your component use this promise, where ever you call it..
handleSubmit().then(// success code).error(// error code)
};
Upvotes: 0
Reputation: 973
Check this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_functio
You are basically trying to check the state when the error is still not caught, and hence the state has not changed.
If you move your code to the render method you will see that it will works since it will re-render after the state changes. Or you get the state change withing the componentDidUpdate
method.
Upvotes: 1
Reputation: 2170
Because axios.post
is asyc function, and at first fires your if
condition, and after .catch
hook. For fix this try to replace your condition in other place, for example in componentDidUpdate method
.
componentDidUpdate() {
if (this.state.loginError) {
console.log('Error!') //does not work
loginError(target);
this.setState({ loginError: false });
}
}
Upvotes: 1