Immutable

As single source of truth is the bible in JS, allowing to change values could be disastrous. For example, I have a list of clients data requested from server, and I pass it to two components. Component A changes it, but component B doesn’t know about it. What’s worse, the original client list is gone, for ever, until you reset it with another remote request.

How ImmutableJS does is wrap (or encapsulate) everything into it’s own Map, or object, like Java. Plus, it makes all properties private, and only provides getters and setters. What’s so good is, setters don’t set, they return a brand new object with the new value you want! So basically you can’t change any value of the original object once it’s wrapped by ImmutableJS.

How React is related?

All you are dealing with in React are props and state. Let’s have a quick recap.

props: Read only, passed from the parent. Even you can assign a different value to a prop, it doesn’t change the view, it does nothing. This is very important.

state: Local variables, or I should call it, constants. The reason is, assigning value to the state doesn’t mean anything either. It doesn’t update the view, and the value is only changed locally to your scope (within the function you update it).

OKay, if you still guessing what I am trying to say, then you’d probably keep reading.

As a experienced React Developer, I’ve never used, or seen assigning values to props, or state, except for some experiments. If you are doing it, STOP, before you get caught! Although, I truly believe, every React developer would stop doing it very soon enough in their React journey, or the journey itself.

That comes to my point of view: you never assign value to props or state in React, only which that matters.

What about the other variables you create outside of the lifecycle?

My question right back to you: do they matter?

I’m afraid not. If you try to use a variable to update the view, instead of those two cases, you will struggle, and eventually get frustrated. Otherwise, you are not using React’s feature.

Despite that React doesn’t have constraints on assigning value to props or state, you will not like to do it, you will not want to do it, and you will not do it.