- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的 reducer 的样子:
export default function catalogReducer(state = initialState.catalogItems, action){
switch (action.type) {
case types.LOAD_CATALOG_SUCCESS:
return {
count:action.count,
products :[...state['products'],
action.catalogItems ?
[...action.catalogItems['products']]
:
[]
]
default:
return state;
}
}
我想做的事情:
这是一个用于加载目录项的 reducer
项目来自分页 api。
问题:
我希望 state 参数应该与之前的状态相关联,但没有,它总是从 initialState 获取值,我在应用程序启动时用它来初始化状态。
这是为什么呢?解决方法是什么?
基本上我需要通过分页 api 返回完整的项目集,而不仅仅是 products
数组中给定页面的产品。我最终得到的只是上次 api 调用的产品数组,因为 state.products
总是被初始化为一个空数组
此外,一般来说,我对在 React 和 Redux 中处理分页 API 感到非常困惑,无法找到一个简单的示例。
将不胜感激。
这是 initialState.js 文件的样子:
import cookie from 'react-cookie';
export default {
categories:[],
sessionId: cookie.load('sessionId') || '',
youMayLikeItems : [],
catalogItems: {'products': []}
}
//这个文件是为了集中store中状态的初始化
最佳答案
基本上,您需要记住您不应该改变状态。
export default function catalogReducer(state = initialState, action) {
switch (action.type) {
case types.LOAD_CATALOG_SUCCESS:
return {
...state,
catalogItems: {
count: action.count,
products: [
...state.catalogItems['products'],
action.catalogItems ?
action.catalogItems['products']
:
{}
]
}
default:
return state;
}
}
您可以找到更多有用的示例 here
此外,最好在没有深层对象的情况下制作简单状态(docs)并使用 Immutable.js
关于reactjs - redux 中 reducer 中的先前状态与 react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41909566/
我的应用将 SceneKit 内容的“页面”与图像和文本交替。当我从图像页面前进到新的 SceneKit 页面时,前一个 SceneKit 页面中的内容会短暂显示,然后被新内容替换。时髦。 我只使用一
我正在尝试处理(在 C# 中)包含一些数字数据的大型数据文件。给定一个整数数组,如何对其进行拆分/分组,以便如果下一个 n(两个或更多)是负数,则前一个 n 元素被分组。例如,在下面的数组中,应该使用
刚接触promises,研究过。所以我的代码和我的理解: sql.connect(config).then(function(connection) { return connection.req
目前我在 if (roobaf) block 中有一些代码,这取决于 foo 和 bar 是否为假。我可以在 block 内再次检查这些条件,但感觉像是不必要的代码重复。 if (foo) {
我是一名优秀的程序员,十分优秀!