After occupying myself a bit with learning React I still consider many of the concepts as hard to get.
Among other: The immutability of props.
If I get it right then components are, more or less, the equivalent to objects in object-oriented programming.
In object-oriented programming you pass data into objects via method-parameter. In React you got props for passing data into components.
If you pass a parameter to a Java-method then you can change these data within the method-body. No problem.
In React not possible because props are immutable.
All literature and stuff I've seen mentions these immutability as a important concept. But so far nobody have really told me why.
Can someone please tell me: What's the great benefit of having immutable props?
Or respectively: What would be the great disadvantage of not having immutability? What could happen if props are mutable instead?
Preferable as a good example. Then chances are bigger that I perhaps get it.
Importance of Immutability in JavaScript If we talk about the frontend library React, Vue os state management library Redux then immutability can also help achieve better performance by enabling quick and cheap comparisons between versions of the state before and after changes.
Let's come to the main question “Are React props immutable?” No, it's not completely true. React props are shallow immutable. It will change the original propertyName in the Parent component also.
Immutable. js is a library that supports an immutable data structure. It means that once created data cannot be changed. It makes maintaining immutable data structures easier and more efficient.
I'll keep it short as I am no expert in functional programming. I'm sure someone with far more experience will pitch in eventually.
Start off by thinking of your components, not in terms of Objects but instead, as functions (as in the mathematical term). Your component is a function of its properties. More specifically it's a render function. It takes props and returns HTML (actually it returns virtual dom trees, but that's irrelevant)
Functions in mathematics are pure. You give them an input, they give you an output. They do not have side effects and they do not use any other inputs. And that gives you some great benefits:
Those are just some benefits, an average developer like myself can see. I'm sure someone with real functional programming experience can come with tons more. Hope it helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With