reactjs - 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;
解决方案
你可以这样做:
const reducer = (state = initialState, action) => {
switch (action.type) {
case "Update":
return { ...state, myvalues: action.payload }
default:
return state;
}
};
当你发送它时,你应该把你的 API 数据放在payload
.
推荐阅读
- cucumber - 空手道测试框架中是否提供数值比较?
- android - Android重力不适用于旧版本的drawable
- c++ - 在 linux 上部署 Qt5.10 App 时出错
- angular - primeNG 多选下拉列表在删除然后再次动态添加相同的值后保留复选框值
- python - Flask + SQLAlchemy:为表创建索引列
- moodle - 带有登录名和密码的 Web 服务
- python - 多索引索引中的不一致行为
- javascript - Google 自定义搜索占位符文本在 Edge 浏览器中不起作用
- ios - 在 Objective-C 中将 JSON 数据转换为 NSString JSON 序列化?
- css - 为什么动画缩放和使用 css 变量进行翻译的处理方式不同?