question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What's the advantage of immutability-helper?

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kolodnycommented, Jan 18, 2017

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:

   state.books[3].showIntro === false
newState.books[3].showIntro === true
state.books[0] === newState.books[0]
state.books[1] === newState.books[1]
state.books[2] === newState.books[2]
state.books[3] !== newState.books[3]
state.books[4] === newState.books[4]
0reactions
kolodnycommented, Dec 19, 2021

It sounds like the reconciliation is probably the bottleneck, you may want to look into memoing your components or trying other react perf tricks

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found