What's the advantage of immutability-helper?
See original GitHub issueI write some code with immutability-helper
, but I have not experience its advantage yet.
Here is some code:
case 1: process the load more of list.
_loadMore() {
const {page, query, filter: {title}} = this.state;
const nextPage = page + 1;
this._fetchData(query, nextPage).then(data => {
const {books} = data;
//create new ref way
const oldBooks = this.state.books;
const newBooks = Array.prototype.concat.apply(oldBooks, books);
let newState = {
books: newBooks,
page: nextPage
}
//immutable-helper way
// let newState = update(this.state, {
// books: {$push: books},
// page: {$set: nextPage}
// });
this.setState(newState, this._refreshScroller);
})
}
experience: maybe update
function is little convenient.
case 2: add a field to some object of list.
_toggleIntro(index) {
//1. use immutable-helper
const newState = update(this.state, {
books: {$apply: books => {
let target = books[index];
const newTarget = update(target, {
showIntro: {$apply: showIntro => {
if(typeof showIntro === 'undefined') {
return true;
} else {
return !showIntro;
}
}}
})
//still need es7 array/object spread property here?
return [...books.slice(0, index), newTarget, ...books.slice(index + 1)];
}}
})
// 2. use es7 object spread property to create new copy
// const {books} = this.state;
// const target = books[index];
// const showIntro = target.showIntro;
// const newState = {
// ...this.state,
// books: [
// ...books.slice(0, index),
// Object.assign({}, target, {showIntro: typeof showIntro === 'undefined' ? true : !showIntro}),
// ...books.slice(index + 1)
// ]
// }
console.log('this.state', this.state); //both of the ways do not change the `source` data.
console.log('newState', newState);
this.setState(newState);
}
experience: still, maybe update
$apply
, $push
, $merge
are convenient?
You don’t have to create some temporary variable to store your new copy?
Thanks for answer.immutability-helper
equal with create new copy
? Am I right?
------- update: --------
Maybe $push, $set, $splice, $merge, $apply
just are the packaged of CURD
operator?
Like the methods ImmutableUpdatePatterns talk about?
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Pros and Cons of using immutability with React.js
Pro: It makes certain features easier One of the side effects you get with immutable data structures is that you always get a...
Read more >What's the advantage of using $splice (from immutability ...
What's the advantage of using $splice (from immutability-helper) over filter to remove an item from an array in React? Ask Question. Asked 5 ......
Read more >Immutability Helpers - React
Overview. React lets you use whatever style of data management you want, including mutation. However, if you can use immutable data in performance-critical ......
Read more >Meet Immer.js – the immutability helper - Scalac
If you have to handle changes in very big objects, immer.js will speed things up. Learn about this powerful tool and immutability helper....
Read more >Immutability in React: Should you mutate objects?
What is immutability? ... If something is immutable, we cannot change its value or state. Although this may seem like a simple concept,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m not sure I understand. Changing the book object directly would change the original state object which is not something you are supposed to do. In the example I gave above, the following statements are true:
It sounds like the reconciliation is probably the bottleneck, you may want to look into memoing your components or trying other react perf tricks