Cannot read property '.then' of undefined when testing async action creators with redux and react

后端 未结 4 1317
执念已碎
执念已碎 2020-12-29 22:38

I\'m trying to write some test using react, redux-mock-store and redux, but I keep getting and error. Maybe because my Promise has not yet been resolved?

<
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-29 23:02

    Your action creator should return a promise as shown below:

    // actions/index.js
    import axios from 'axios';
    
    import { FETCH_LISTINGS } from './types';
    
    export function fetchListings() {
      return (dispatch) => {
        return axios.get('/5/index.cfm?event=stream:listings')
        .then(( { data } ) => {
          dispatch({ type: FETCH_LISTINGS, payload: data });
        });
      }
    };

提交回复
热议问题