Redirect after post in api with Reactjs files

Asked

Viewed 481 times

0

Good morning,

I am trying to create a login screen in Reactjs where a post is done on an api, after sending the user email and password, the API returns the JWT authentication token.

API post in my component (Login.js)

 axios({
            method: 'post',
            url: 'https://localhost:44332/api/token',
            data: corpo,
            config: { headers: { 'Content-Type': 'multipart/form-data' } }
        })
            .then(function (res) {

                // salva o token no localstorage
                localStorage.setItem('token', res.data.token);


            }).catch(function (response) {
                console.log(response);
            });

Is this the right way to do it? How can I redirect my user to another component. ex: Dashboard.js

Grateful, abs

1 answer

1


If you are using react-router-dom you can use this.props.history.push('/your-path'). /your-path is the path you set in your routes that renders the Dashboard component.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.