Use Object.assign to replace Redux isomorphic object
Blogs20172017-04-01
Use Object.assign to replace Redux isomorphic object
let state = {
stars: 5,
list: [1,2,3,4,5],
comment: { user: 'weber', note: 'cyberspace' }
}
//1. replace scalar variable:
var s1 = Object.assign({}, state, { stars: 8 });
console.info('1: ', JSON.stringify(state), JSON.stringify(s1));
//2. replace array:
var s2 = Object.assign({}, state, { list: state.list.slice(2) });
console.info('2: ', JSON.stringify(state), JSON.stringify(s2));
// 3. replace object:
var comment = state.comment
var s3 = Object.assign({}, state, {
comment: {
user: 'what-ever-user',
note: comment.note
}
});
console.info('3: ', JSON.stringify(state), JSON.stringify(s3));In every case, the original state keeps unchanged, and new generated object is updated, which is as expected.
