Redux docs recommends you to use the spread operator approach instead of the Object.assign
As per the docs:
An alternative approach is to use the object spread syntax proposed
for the next versions of JavaScript which lets you use the spread
(...) operator to copy enumerable properties from one object to
another in a more succinct way. The object spread operator is
conceptually similar to the ES6 array spread operator
The advantage of using the object spread syntax becomes more apparent when you're composing complex objects
Using the Spread operator syntax
export const selectDiameter = (scaleData,size) => {
return {
type: SELECT_DIAMETER,
payload: {...scaleData,
diameter: {
...scaleData.diameter,
selectedTube: size,
isClicked: true,
audio: {
...scaleData.diameter.audio,
isPlayed: true
}
}
}
}
}
It still uses ES6. See the Redux docs for more info on configuring your code for the same
However when you are dealing with the nested data. I prefer to use immutability-helpers
For your code it will be like
import update from 'immutability-helpers';
export const selectDiameter = (scaleData,size) => {
return {
type: SELECT_DIAMETER,
payload: update(scaleData, {
diameter: {
selectedTube: { $set: size},
isClicked: { $set: true},
audio: {
isPlayed: {$set: true}
}
}
})
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…