8

the documentation says All React components must act like pure functions with respect to their props. https://facebook.github.io/react/docs/components-and-props.html, but does not explain the real reason behind it, why is that?

rahulaga-msft
  • 3,964
  • 6
  • 26
  • 44
nomadus
  • 859
  • 1
  • 12
  • 28

4 Answers4

3

A React component should be pure, this means the result of its render method should depend solely on the props and the state, and for the same properties and state render should give the same result.

If render is not pure, it means it can return different results for the same input, so React cannot tell which parts of the DOM need to be updated based on the changes to the component. This is critical as the performance of React depends of this. Why? That's a bit complex.

It is amazing to define the UI based on a state, and have the UI re-render itselfs every time any part of the state changes. But you can imagine doing a complete re-render of the entire DOM every time you change something would be painfully slow. React solves this by checking the minimum ammount of changes needed o the DOM to reflect the new state. It knows what those changes are based on what properties and state each component receives, and can tell if it needs to update a component if any of its properties or state changed.

Take this tree as an example of a component hierarchy

State tree

Here we changed h to 8, so we also changed f because h is a child of f, and we also changed c because f is child of c and etcetera.

The key here is to think how React checks the component tree. It ill start at the root and see it changed. Then it'll check all the children and realize only c changed, so there is no need to check all the a and b branches. Then it'll check the c branch and realize only f changed so there is no need to check e and g. That operation is done on every component to calculate the minimum ammount of changes and also what needs to be updated.

If at any point you could mutate how a component is rendered it means React will need to check all of the branches and all of its children to know what changed because it can't rely on the state and the props to know when a branch changed and how. This would be painfully slow and make the whole React framework inviable.

Marco Scabbiolo
  • 7,203
  • 3
  • 38
  • 46
  • Hi Marco, thanks for the answer. I see that if props do not change this leads to better performing diffing algorithm, but on the other hand React deals with the state, which changes. So with props we get better performance but with the state we don't? – nomadus Feb 02 '17 at 11:48
  • Both state and props are taken into account in the diffing algorith, so it should be the same performance in that regard. I think the state vs props discussion is more related to conventions and arquitecture patterns than to performance. – Marco Scabbiolo Feb 02 '17 at 15:52
1

I would say because of tracking the component state changes. If it isn't pure, it would cause side-effects every time it is executed. That way, would be very hard to know what has changed and furthermore how to react to these changes.

Pure functions, in other way, have the same output with the same input. Making it a lot easier to manage properties and track when something has changed, resulting a easier and predictable way to react to the change.

Diego Faria
  • 8,805
  • 3
  • 26
  • 33
1

If they weren't pure functions in relation to their props then it would be violating the entire heirarchy/delegation structure that react provides and relies on.

Lets say you have two components, component A and Component B, and Component A is the parent to Component B. Component A has its own state based on some sort of data. When you pass a part of its state down as a prop to component B, you are establishing a contract between the two components that component B will delegate to component A to get the value of said prop.

This is in a sense a contract between the two components and the only way the contract isn't violated is that component B doesn't directly alter or change the passed down prop. That is what being a pure function means, that it doesn't mutate the prop directly. Of course you can clone the prop and then change it however you want that isn't a breaking of contract since at that point they aren't referencing the same values. But if you do mutate props directly you will also be mutating the parent component value. This can cause unintended side effects as well as cause issues with the react shadow dom differencing algorithm.

Here is that explained from the official react docs

https://facebook.github.io/react/blog/2015/02/24/streamlining-react-elements.html#problem-mutating-props-you-dont-own

finalfreq
  • 6,830
  • 2
  • 27
  • 28
0

You will discoverer "Why" understanding Reconciliation algorithm React uses for rendering.

Here you have all the information needed to understand what you want.

Part of that is well explained in Marco Scabbiolo's answer, but If you want to understand the way React works I strongly recommend you to read the post I've suggested.

Posting the answer here would be too much for a post and unnecesary because It has already explained by React Team. That's why I prefer giving you the source directly.

Facundo La Rocca
  • 3,786
  • 2
  • 25
  • 47