我想使用react和redux编辑数组中的一个元素。
我的问题是我设置了一次我映射的数组的状态。在这张地图中,我尝试用reducer改变这个元素。
有可能吗?我尝试使用Object.assing()来避免改变状态,但我必须改变状态。这不是真的吗?
在reducer下面:
let obj = {
"name" => "keshav",
"prof" => "Engineer"
}
容器:
import * as actionTypes from '../actions';
const iniState = {
components: []
};
const rootReducer = (state = iniState, action) => {
switch (action.type) {
case actionTypes.ADD_COMPONENT:
const newComponent = {
id: Math.random(),
co: action.data.compToReducer
}
return {
...state,
components: state.components.concat(newComponent)
};
case actionTypes.DELETE_COMPONENT:
return {
...state,
components: state.components.filter(component=> component.id !== action.index)
}
case actionTypes.EDIT_COMPONENT:
return
Object.assing({}, state, {co: state.co = action.data.componentToReducer})
}
return state;
}
export default rootReducer;
答案 0 :(得分:0)
onEditComponent: (component, id) => dispatch({type: actionTypes.EDIT_COMPONENT, data:{componentToReducer: component, index: id}})
<button onClick={this.props.onEditComponent}>
Edit Component
</button>
当您尝试将SyntheticEvent传递给reducer时,这不起作用。回调执行后的合成事件get nullified。