【发布时间】:2016-04-23 21:53:59
【问题描述】:
我正在尝试在 redux docs' basic example 中复制类似于 TodoList 示例的内容。第二个 reducer 接收一个数组 - styleItems = [{... ... }, {... ...}] - 然后调用第一个函数对每个单独的对象进行操作。
我通过以下方式向应用容器提供initialState,如containers/app.js 所示。但是,传递给 styleItems 减速器的状态似乎是一个空白数组 - 每次都是。
但是,react 会根据初始配置呈现 UI,并且 react dev-tools 会按预期显示状态结构。 redux 商店是否以某种方式看到与 react 相同的东西?
containers/app.js
function starterInfo(state) {
return {
// The ID of this particular object
id: 12345,
// Various keys and theri css values
styleItems: [
{
pk: 31,
order: 1,
label: 'Caption text color',
css_identifier: '.caption-text',
css_attribute: 'color',
css_value: '#FFFFFF'
},
{
pk:23,
order: 2,
label: 'Caption link color',
css_identifier: '.caption-link',
css_attribute: 'color',
css_value: '#FEFEFE'
}
],
// Network state info
currently_fetching: false,
currently_posting: false
}
}
export default connect(starterInfo)(App)
reducers/index.js
// This handles a single styleItem object within the array
function change_css(state = {}, action){
switch (action.type){
case actions.CHANGE_CSS:
if (state.order !== action.order){
return state
}
return {
...state,
css_value
}
default:
return state
}
}
// This handles the styles array in the global state
function styleItems(state = [], action){
switch(action.type){
case actions.CHANGE_CSS:
const foobar = state.map(styleItem =>
change_css(styleItem, action)
)
return foobar
default:
return state
}
}
【问题讨论】:
标签: javascript reactjs redux redux-devtools