Skip to content

Commit 9e380c2

Browse files
committed
Merge pull request #5849 from david0178418/master
Added additional detail to "props-in-getInitialState" anti-pattern doc (cherry picked from commit f1c1544)
1 parent 7594b97 commit 9e380c2

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

docs/tips/10-props-in-getInitialState-as-anti-pattern.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ next: dom-event-listeners.html
1111
>
1212
> This isn't really a React-specific tip, as such anti-patterns often occur in code in general; in this case, React simply points them out more clearly.
1313
14-
Using props, passed down from parent, to generate state in `getInitialState` often leads to duplication of "source of truth", i.e. where the real data is. Whenever possible, compute values on-the-fly to ensure that they don't get out of sync later on and cause maintenance trouble.
14+
Using props to generate state in `getInitialState` often leads to duplication of "source of truth", i.e. where the real data is. This is because `getInitialState` is only invoked when the component is first created.
15+
16+
Whenever possible, compute values on-the-fly to ensure that they don't get out of sync later on and cause maintenance trouble.
1517

1618
**Bad example:**
1719

@@ -43,7 +45,7 @@ ReactDOM.render(<MessageBox name="Rogers"/>, mountNode);
4345

4446
(For more complex logic, simply isolate the computation in a method.)
4547

46-
However, it's **not** an anti-pattern if you make it clear that synchronization's not the goal here:
48+
However, it's **not** an anti-pattern if you make it clear that the prop is only seed data for the component's internally-controlled state:
4749

4850
```js
4951
var Counter = React.createClass({

0 commit comments

Comments
 (0)