reactjs - Redirect to home path after successful login | React, Redux -


hello people @ stack overflow! want user redirected when he/ has been logged app. can't work. thought add promise in action after has been logged in, apparently not.

here's authentication action:

import axios 'axios' import settings '../settings' import { withrouter } 'react-router-dom'  axios.defaults.baseurl = settings.hostname  export const login = ({ email, password }) => {   return dispatch => {     return axios       .post('/tokens', { email: email, password: password })       .then(response => {         this.props.history.push('/') // not working :(       })   } } 

when inspect app in developer console valid response, this: unhandled rejection (typeerror): cannot read property 'history' of undefined.

thanks reading!

you can add callback function:

import axios 'axios' import settings '../settings' import { withrouter } 'react-router-dom'  axios.defaults.baseurl = settings.hostname  export const login = ({ email, password }, callback) => {   return dispatch => {     return axios       .post('/tokens', { email: email, password: password })       .then(response => {          //this.props.history.push('/') // not working :(          // stuff here ...       })       callback();   } } 

and in component:

this.props.login({email, password}, () => {         this.props.history.push('/dashboard');     }); 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -