简体   繁体   中英

Why am i still getting cannot read map of undefined?

Running redux sagas and react together and totally stumped why i'm still getting undefined errors. I have an initial state set so that .map can just run on that, i've tried null, undefined, empty arrays - always the same error.

Where am I going wrong, guys & gals?

App.js

    {
      isFetching ? (
        <button disabled>Fetching...</button>
      )
      :
      <div>
        <button onClick={onRequest}>Click For API Names</button>
        <ul>
          {users.map((each, i) => <li key={i}>{each.name}</li>)}
        </ul>
      </div>
    };

...
const mapStateToProps = state => {
  return {
    isFetching: state.isFetching,
    users: state.users,
    error: state.error,
  };
};

const mapDispatchToProps = dispatch => {
  return {
    onRequest: () => dispatch({ type: constants.API_REQUEST }),
  };
};

reducer.js

const initialState = {
  isFetching: false,
  users: [{ name: '' }, { name: '' }],
  error: null,
};

export const namesReducer = (state = initialState, action) => {
  switch(action.type) {
    case constants.API_REQUEST:
      return {
        ...state,
        isFetching: true,
        error: null,
      };
    case constants.API_SUCCESS:
      return {
        ...state,
        isFetching: false,
        users: action.payload,
        error: null,
      };
    case constants.API_FAILURE:
      return {
        ...state,
        isFetching: false,
        users: [''],
        error: action.error,
      };
    default:
      return state;
  };
}

users: action.payload

action payload probably isn't an array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM