--- id: props-in-getInitialState-as-anti-pattern title: Using State to Cache Calculations Is an Anti-Pattern layout: tips permalink: props-in-getInitialState-as-anti-pattern.html prev: componentWillReceiveProps-not-triggered-after-mounting.html next: dom-event-listeners.html --- > Note: > > 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 state to cache values calculated from props (for example 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. Bad example: ```js /** @jsx React.DOM */ var MessageBox = React.createClass({ getInitialState: function() { return {nameWithQualifier: "Mr. " + this.props.name}; }, render: function() { return
{this.state.nameWithQualifier}
; } }); React.renderComponent(, mountNode); ``` Better: ```js /** @jsx React.DOM */ var MessageBox = React.createClass({ render: function() { return
{"Mr. " + this.props.name}
; } }); React.renderComponent(, mountNode); ``` For more complex logic: ```js /** @jsx React.DOM */ var MessageBox = React.createClass({ render: function() { return
{this.getNameWithQualifier(this.props.name)}
; }, getNameWithQualifier: function(name) { return 'Mr. ' + name; } }); React.renderComponent(, mountNode); ```