为什么我仍然无法读取未定义的地图?

时间:2018-03-08 20:45:37

标签: javascript reactjs redux redux-saga

运行redux sagas并做出反应并完全难以理解为什么我仍然会遇到未定义的错误。我有一个初始状态设置,以便.map可以运行,我已经尝试了null,未定义,空数组 - 总是相同的错误。

我哪里出错,男人和女人加仑?

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;
  };
}

1 个答案:

答案 0 :(得分:3)

用户:action.payload

动作有效负载可能不是数组