Tools and libraries
In the next section, we will go through some techniques, tools, and libraries that we can apply to our codebase to monitor and improve the performance.
Immutability
As we have seen, the most powerful tool we can use to improve the performance of our React application is the shouldComponentUpdate
using the PureComponent
.
The only problem is that the PureComponent
uses a shallow comparison method against the props and state, which means that if we pass an object as a prop and we mutate one of its values, we do not get the expected behavior.
In fact, a shallow comparison cannot find mutation on the properties and the components never get re-rendered, except when the object itself changes.
One way to solve this issue is using immutable data: Data that, once it gets created, cannot be mutated.
For example, we can set the state in the following mode:
const obj = this.state.obj obj.foo = 'bar' this.setState({ obj })
Even if the value of the foo
attribute of the object is changed...