我有一个reduxreducer,看起来像这样:
const initialState = {
visitedData:{specialty:false,review:false,reviewUpgrade:false}
}
const myred= (state = initialState, action) => {
switch (action.type) {
case 'GO_FOR_IT_SON':
return Object.assign({}, state, { visitedData: action.data });
default:
return state
}
}
现在从我的reactcomponent我会打电话给:
store.dispatch({type: 'GO_FOR_IT_SON', data:{review:true} });
或:
store.dispatch({type: 'GO_FOR_IT_SON', data:{specialty:false} });
因此,这些语句中的每一个都应该将visitedData的一个属性设置为true / false,并使其他语句保持不变。 如何将visitedData的每个属性设置为true / false并保持其他属性不变?
答案 0 :(得分:0)
我建议为每个changable属性设置一个reducer和action:
import { combineReducers } from 'redux';
const specialty = (state = false, action) => {
if (action.type === 'TOGGLE_SPECIALTY') {
return action.data;
}
return state;
};
const review = (state = false, action) => {
if (action.type === 'TOGGLE_REVIEW') {
return action.data;
}
return state;
};
const myred = combineReducers({
specialty,
review
});
但在您的情况下,解决方案将是:
const myred= (state = initialState, action) => {
switch (action.type) {
case 'GO_FOR_IT_SON':
return Object.assign({}, state, {
visitedData: Object.assign({}, state.visitedData, action.data)
});
default:
return state
}
}
答案 1 :(得分:0)
我认为这会奏效:
return Object.assign({}, state, {
visitedData: Object.assign({}, state.visitedData, action.data)
});
检查此示例:
let a = {b: {a : 1, b : 2, c : 5} };
let b = {a : 5};
let k = Object.assign({}, a, {b: Object.assign({}, a.b, b)});
console.log(k);

答案 2 :(得分:0)
与其他解决方案相同。我建议将对象合并分成另一行,并使用对象扩展来获得更多可读性
const myred = (state = initialState, action) => {
switch (action.type) {
case 'GO_FOR_IT_SON':
let newVisitedData = {
...state.visitedData,
...action.data
}
return { visitedData: newVisitedData }
default:
return state
}
}
答案 3 :(得分:-1)
直接的蛮力方式:
编辑使用lodash cloneDeep
Sub Macro4()
Sheets("Sheet1").Select
Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
'Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet3").Select
ActiveCell.SpecialCells(xlLastCell).Select
Selection.Offset(1, 0).Select
Selection.End(xlToLeft).Select
ActiveSheet.Paste
Sheets("Sheet2").Select
Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
'Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet3").Select
ActiveCell.SpecialCells(xlLastCell).Select
Selection.Offset(1, 0).Select
Selection.End(xlToLeft).Select
ActiveSheet.Paste
End Sub