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

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -