React-router: React Router Redirect does not work

Created on 11 Jul 2017  路  1Comment  路  Source: ReactTraining/react-router

After I log in react router should redirect me to x route, But not in my case, here is a code of submit.

handleSubmit(e) {
        e.preventDefault();
        this.setState({
            isSubmitting: true,
        })
        axios.get(`/api/v1/auth/`, {
            params: {
                email: this.state.email,
                password: this.state.password
            }
        })
            .then((response) => {
                if (response.data.token) {
                    localStorage.setItem("Authorization", `Bearer ${response.data.token}`);
                    console.log('Ok Redirecting');
                    <Redirect to="/forum" />
                }
                this.setState({
                    errorOccurred: true,
                    isSubmitting: false,
                    errorMessage: response.data.error
                })
            }).catch(function (error) {
            this.setState({
                errorOccurred: true,
                isSubmitting: false,
                errorMessage: 'Unknow error'
            })
        });

    }

As you can see I expect response.data.token and if everything is ok I would like to redirect the user back to /forum route. I do get console.log'Ok Redirecting' but after that redirection does not take place.
I'm importing only Redirect component from react-router-dom.

>All comments

This is a bug tracker, not a support system. For usage questions, please use Stack Overflow or Reactiflux. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

imWildCat picture imWildCat  路  3Comments

winkler1 picture winkler1  路  3Comments

alexyaseen picture alexyaseen  路  3Comments

andrewpillar picture andrewpillar  路  3Comments

Waquo picture Waquo  路  3Comments