Categories
Learning

Immutability

So this is a concept that I have known for a while but have not been sure why it’s such a good idea. After going through a few documents and watching a 30 min talk I think I have a better understanding of what it’s all about.

Basically, you do not want your object to mutate. Because when you are not sure what has changed. For example,

let A = 1;
doSmth(A);
console.log(A); // What is A here?

It totally depends on what the function does and it is difficult to track how the values have changed over time. The library that I read up on was immutableJS, this library uses a Directed Acyclic Graph internally to represent the values internally. It makes sense because DAG will never point to a node causing a completed graph. The implementation is cool but shall not go into that.

So basically, the benefit of using an immutable data structure is that it allows me to compare between two objects much more efficiently. Because of the way DAG works, it can answer the question of “what changed?” very quickly. You can ensure that updating a variable do not cause weird bugs in a different portion of your application, because it’s not the “same” variable.

If it seems a bit confusing, it’s because it is and I’m dying. To the point where I actually need to write things down like these to remember what the hell I’ve been learning.

Till the next puzzle piece.

Categories
Learning

Redux: action creators

Because of the sheer amount of information that I’ve been researching on lately, I need to write it down somewhere before I forget all the shit that I’ve learnt.

Action creator is best used with the container-component pattern (smart/dumb). Basically the action creator separates the responsibility of creating the action within the component itself when you need to dispatch the action. Instead, you split it away as a function, and call it whenever you require to use it.

This is why it’s great to use this with mapDispatchToProps because it just works well together and you’re just passing a function from the smart to dumb.