cannot get a promise after bindActionCreators in Redux -


i use react/redux create app.

i've custom action creator make async request (i use redux-thunk).

export function loginattempt(userdata) {   return dispatch => {      let formdata = new formdata();     formdata.append('username', userdata.username);     formdata.append('password', userdata.password);      fetch('https://api.t411.ch/auth', {       method: 'post',       body: formdata     }).then(response => {       if (response.status !== 200) {         const error = new error(response.statustext);         error.respone = response;         dispatch(loginerror(error));         throw error;       }       return response.json();     }).then(data => {        dispatch(loginsuccess(data));     });   } 

in component, use bindactioncreators bind method dispatch :

import react, { component } 'react'; import { connect } 'react-redux'; import { bindactioncreators } 'redux';   import searchbar './searchbar'; import torrentlayout './torrentlayout';  import * loginactions '../actions/login'; // <---- it's code above located import * searchactions '../actions/search';    function mapstatetoprops(state) {   return {     login: state.login,     searching: state.searching   }; }  function mapdispatchtoprops(dispatch) {   return bindactioncreators({...loginactions, ...searchactions}, dispatch); }  @connect(mapstatetoprops, mapdispatchtoprops) export default class home extends component {    constructor(props) {     super(props);      console.log('should promise');     let foobar = this.props.loginattempt({username: 'username', password:'password'});      console.log(foobar); // <------ undefined      // want     this.props.loginattempt({username: 'username', password:'password'}).then(() => {         this.props.search(this.props.login.token, "mysearch");     }   }    render() {     return (       <div>         <div>            <searchbar {...this.props} />            <torrentlayout {...this.props}/>         </div>       </div>     );   } } 

i apply 'then' action creator bound dispatch.

thanks

you need return fetch() inside arrow function inside loginattempt. so:

export function loginattempt(userdata) {   return dispatch => {     return fetch('https://api.t411.ch/auth', {       method: 'post',       body: formdata     }).then(...);   } 

basically when call binded action creator arrow functions gets executed doesn't have return value.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -