Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Produces Immutable Data and Avoid Unnecessary Creation of New Data Trees with Immer

    Michel WeststrateMichel Weststrate

    In the immutable data paradigm, it is quite important that we don’t accidentally change the data somewhere. It would break the paradigm in ways that are very hard to debug. In this lesson we’ll discover how Immer can help us by automatically freezing all data that is produced.

    Furthermore, when writing reducers by hand it is quite easy to accidentally create new state trees, where this wasn’t needed. We will discover how Immer avoids this problem.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Instructor: Immer doesn't just simplify producing the next immutable state from the current state. It does a few things more. We're going to refer to our original implementation of toggle reservation.

    Here, we have a test, in which we tried to change the reservation of the immutable state, which shouldn't be allowed because that makes it, by definition, mutable, not immutable. What we see is that this is possible. This doesn't trail any exception.

    After this happens somewhere in your code base, these are reactions, which are really hard to track down. Secondly, there's another limitation. If we reserve an already reserved GIF, if you look at its implementation, that is still going to produce a new GIF, although it will have to say, "Reference to the same person," which is a bit shame. It means that for example, the component would still be re-rendering if we were able to press a button.

    Let's capture that in a unit test as well. First, we check if the GIF that we have in a new state is exactly deeply equal to the previous states, which is the case. It's the object has the same shape.

    However, if you're wondering, is it the same object still? Then we see that that's assertion fails. It's not the same GIF anymore. It's not the same object, even though we didn't change it in any way.

    If it's not the same object, that also means that it's not the same GIFs collection anymore, that it's not the same state anymore. This is the real culprit. If the initial state is not the next state anymore, it means that on a lot of levels our inaudible we could apply otherwise, aren't applies anymore.

    Let's return to our Immer base implementation, save it. Now, we see that out tests are passing. That's this modification actually draws an exception. It also means that every try to reserve more additional GIFs, nothing changes and we literally end up with the very same state as we started with.

    Now, freezing objects is expensive in JavaScript. It can be turned off by using the sets autofreeze API to false. Now our tests will fail because Immer no longer freezes our states. However, in normal circumstances, you don't have to configure this. Why? Because Immer only turns this feature on in developments builds. In prediction builds, freezing is automatically turned off.

    These features are made possible by the fact that Immer behaves like our secretary. Our secretary can do a lot of processing such as freezing, such as checking everywhere truly changes...