Skip to content

Commit

Permalink
Merge pull request facebook#5849 from david0178418/master
Browse files Browse the repository at this point in the history
Added additional detail to "props-in-getInitialState" anti-pattern doc
  • Loading branch information
zpao committed Jan 14, 2016
2 parents fcc63e1 + fb5cd2f commit f1c1544
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions docs/tips/10-props-in-getInitialState-as-anti-pattern.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ next: dom-event-listeners.html
>
> 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.
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.
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.

Whenever possible, compute values on-the-fly to ensure that they don't get out of sync later on and cause maintenance trouble.

**Bad example:**

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

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

However, it's **not** an anti-pattern if you make it clear that synchronization's not the goal here:
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:

```js
var Counter = React.createClass({
Expand Down

0 comments on commit f1c1544

Please sign in to comment.