4

I was searching for an answer to another problem and I found this answer (link) I'm just curious why is it that modifying its own props is an anti-pattern, and why isn't it that modifying its own state is not and anti-pattern?

Community
  • 1
  • 1
Mike W
  • 1,303
  • 1
  • 21
  • 31
  • See: http://stackoverflow.com/questions/26089532/why-cant-i-update-props-in-react-js It's a paradigm the framework uses. – Evan Trimboli Jul 17 '16 at 13:27

1 Answers1

6

In react, props is an object of data that is unlikely to change in the lifecycle of the component, and state is data that is likely to change in the lifecycle of the component.

It helps create explicit rules for where developers should put things that do/do-not change.

React update pages very quickly by knowing exactly that data changes will either come strictly from a parent(as props) or come internally as state.

the exact function checks loosely if state/props are the same, then https://github.com/facebook/react/blob/master/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js#L881-L883

I'd recommend reading about how react "reconciles" data for changes and making updates to the dom. https://facebook.github.io/react/docs/reconciliation.html

Blair Anderson
  • 19,463
  • 8
  • 77
  • 114
  • To add another point - React treats props as immutable such that it can perform certain shallow checking optimizations during the re-render flow and bail out if there's no work to be done. – probablyup Jul 19 '16 at 05:27
  • @yaycmyk it actually checks both https://github.com/facebook/react/blob/master/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js#L881-L883 – Blair Anderson Jul 19 '16 at 06:09