首页 > 解决方案 > Redux-React 使用 api 调用填充 initialState

问题描述

使用来自组件或此处的 json api 调用填充我的 initialState

const initialState = {
      myvalues: [] ---->here i want to populate this array
    };

const reducer = (state = initialState, action) => {
  const newState = { ...state };
  switch (action.type) {
    case "Update":
      console.log(newState);
    // newState.myvalues = action.key.title.value;
    default:
      return newState;
  }
};
export default reducer;

标签: reactjsreduxreact-redux

解决方案


你可以这样做:

const reducer = (state = initialState, action) => {
  switch (action.type) {
    case "Update":
      return { ...state, myvalues: action.payload }
    default:
      return state;
  }
};

当你发送它时,你应该把你的 API 数据放在payload.


推荐阅读