title: React JS this.props.data not defined in getInitialState for child component even though it is defined in render (and is defined in parent)
body: <p>Scenario:</p>
  
  <ol>
  <li>Re-using components (<code>&lt;Row /&gt;</code> -> <code>&lt;Cell /&gt;</code>) in the same grandparent wrapper (<code>&lt;ShippingTable /&gt;</code> -> <code>&lt;Grid /&gt;</code>) for code re-use. </li>
  <li>For one, I assign an array of data and loop through to re-use child component (<code>&lt;Row /&gt;</code> -> <code>&lt;Cell /&gt;</code>).</li>
  <li>For the second one, it's just an object (with the same properties as the array's objects) which I just assign directly in render (no need for <code>this.props.data.map</code> loop as already just one object).</li>
  </ol>
  
  <p>Problem:</p>
  
  <ol>
  <li>For the array, all works as required. <code>this.props.data</code> is passed down to children, state is updated through various events and all is well.</li>
  <li>However, for the single object, all works fine until <code>&lt;Row /&gt;</code>. Even though this.props.data contains valid values and is properly assigned to the child <code>&lt;Cell /&gt;</code> component, in <code>&lt;Cell /&gt;</code>'s getInitialState, it is inexplicably undefined (or set to whatever initial values set in <code>&lt;ShippingTable /&gt;</code>'s <code>getInitialState</code>). even though, apart from being called in a .map loop, it is <em>exactly the same code and data that works in the same rendering for my array of data</em>.</li>
  <li>Moreover, <code>this.props.data</code> in <code>&lt;Cell /&gt;</code>'s render is indeed present and accurate, but because of the failed setState in getInitialState, this.state.data is undefined (or set to whatever values set in <code>&lt;ShippingTable /&gt;</code>'s <code>getInitialState</code>).</li>
  <li>Double moreover, if I force a UI re-render (e.g. change another state property in the grandparent <code>&lt;ShippingTable /&gt;</code>), everything works as how I would expect it in the first place.</li>
  </ol>
  
  <p>It's as if <code>getInitialState</code> in <code>&lt;Cell /&gt;</code> for only my single percentages object is being called before a successful AJAX population of setState, or it's not being called again when the state is changed after being populated with updated data from the server.</p>
  
  <p>Here's a stripped down (but still long) version of the actual code:   </p>
  
  <p><strong>UPDATE - Solved</strong></p>
  
  <p>Never forget that with React, always send props down, send events up, and always keep state as far up to the top as possible. Removed <code>&lt;Cell /&gt;</code> and <code>&lt;Row /&gt;</code>'s state-tracking responsibilities by moving them back up to the ShippingTable (instead referencing props passed down by parent) where state is actually tracked (and should always be tracked). As per @rallrall below, I got off track and was inexplicably working against the framework. All very clear in retrospect (although why the incorrect approach worked for the array but not the object did muddy the waters - especially when switching the object to an array ended up working as well).</p>
  
  <pre><code>   var ShippingInput = React.createClass({
          getInitialState: function() {
              return { value: this.props.value };
          },
          handleChange: function(event) {
              var value = event.target.value;
              ...conversion to decimal and validation
              this.props.onChange(value);
              this.setState({ value: value });
          },
          render: function() {
              return (
                  &lt;Input type="text" placeholder="0.00" bsStyle={this.validationState()} addonBefore={addOnBefore}
                    value={this.state.value} label={this.props.label} ref="input" onChange={this.handleChange} 
                    groupClassName={this.props.groupClassName} labelClassName={this.props.labelClassName} onKeyDown={this.props.onKeyDown} /&gt;
          );
        }
      });
  
      var Cell = React.createClass({
          propTypes: {
              data: React.PropTypes.number.isRequired,
              onChange: React.PropTypes.func.isRequired,
              onRowEdit: React.PropTypes.func.isRequired
          },
          getInitialState: function() {
              // At this point, this.props.data is undefined *only for &lt;Cell data={this.props.percentages} /&gt; in &lt;Row /&gt; even though that prop is not null there.
              return {value: this.props.data};
          },
          handleChange: function(value) {
              this.props.onChange(value);
              this.setState({ value: value });
          },
          render: function() {
              var edit = this.props.edit;
              var showInput = edit ? 'group-class' : 'group-class hide'; 
              var showText = edit ? 'hide' : 'btn btn-default btn-sm';
  
              var val = this.props.isRates ? accounting.formatMoney(this.state.value) : this.state.value;
  
              // {this.state.value} is undefined here for only the percentages object
              // {this.props.data} is *not undefined* 
              var input = &lt;ShippingInput type="text" label={this.props.label} value={this.state.value} ref="input"
                    isRates={this.props.isRates} groupClassName={showInput} labelClassName="label-class sr-only" onKeyDown={this.handleKeyDown} onChange={this.handleChange} /&gt;;
  
              var text = (&lt;a href="#" className={showText} onClick={this.handleClick}&gt;{val}&lt;/a&gt;);
  
              return ( &lt;td&gt;{input}{text}&lt;/td&gt; );
          }
      });
  
      var Row = React.createClass({
          propTypes: {
              data: React.PropTypes.object.isRequired,
              onCellChange: React.PropTypes.func.isRequired,
              onRowCommit: React.PropTypes.func.isRequired
          },
          getInitialState: function() {
              return {edit: false};
          },
          handleChange: function(prop, val) {
              this.props.onCellChange(prop, val);
          },
          ...
          render: function() {
              var edit = this.state.edit;
              var text = edit ? 'fa fa-save fa-fw' : 'fa fa-edit fa-fw';
              return &lt;tr&gt;
                  &lt;Cell data={this.props.data.Canada} isRates={this.props.isRates}  label="Canada" edit={edit} onRowEdit={this.handleRowEdit} onRowCommit={this.handleRowCommit}  onChange={this.handleChange.bind(null, "Canada")} /&gt;
                  &lt;Cell data={this.props.data.Us} isRates={this.props.isRates}  label="United States" edit={edit} onRowEdit={this.handleRowEdit}  onRowCommit={this.handleRowCommit}  onChange={this.handleChange.bind(null, "Us")} /&gt;
                  &lt;Cell data={this.props.data.International} isRates={this.props.isRates} label="International" edit={edit} onRowEdit={this.handleRowEdit}  onRowCommit={this.handleRowCommit} onChange={this.handleChange.bind(null, "International")} /&gt;
                  &lt;td&gt;
                      &lt;Button href="#" ref="commit" onClick={this.handleRowCommit} bsStyle="primary" bsSize="small"&gt;&lt;span className={text}&gt;&lt;/span&gt;&lt;/Button&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;;
          }
      });
  
      var Grid = React.createClass({
          propTypes: {
              data: React.PropTypes.array.isRequired,
              percentages: React.PropTypes.object.isRequired,
              onCellChange: React.PropTypes.func.isRequired,
              onRowCommit: React.PropTypes.func.isRequired
          },
          render: function() {
              var rows = this.props.data.map(function(rowData, index) {
                  var id = rowData["Id"];
                  return &lt;Row key={id} isRates={true} data={rowData} onCellChange={this.props.onCellChange.bind(null, index)} onRowCommit={this.props.onRowCommit.bind(null, index)} onRowDelete={this.props.onRowDelete.bind(null, index)} /&gt;;
              }, this);
  
              return (
                  &lt;Table striped bordered hover responsive&gt;
                    &lt;thead&gt;
                    &lt;tr&gt;
                      &lt;th className="col-sm-4"&gt;Order Subtotal (up to)&lt;/th&gt;
                      &lt;th className="col-sm-2"&gt;Canada&lt;/th&gt;
                      &lt;th className="col-sm-2"&gt;US&lt;/th&gt;
                      &lt;th className="col-sm-2"&gt;International&lt;/th&gt;
                      &lt;th className="col-sm-1"&gt;&lt;/th&gt;
                    &lt;/tr&gt;
                    &lt;/thead&gt;
                      &lt;tbody&gt;
                          {rows}
                          &lt;tr&gt;&lt;td colSpan="5"&gt;If the order subtotal is greater than the largest amount on the above chart, the following rates apply:&lt;/td&gt;&lt;/tr&gt;
                          &lt;Row key="Percentages" isRates={false} data={this.props.percentages} onCellChange={this.props.onPercentCellChange} onRowCommit={this.props.onPercentRowCommit} /&gt;
                      &lt;/tbody&gt;
                  &lt;/Table&gt;  
  
              );
          }
      });
  
      var ShippingTable = React.createClass({
          getInitialState: function() {
              return this.props.initialData;
          },
          loadFromServer: function() {
              $.getJSON(this.props.url, function(data) {
                  if (!data || data.Success === false) {
                      toastr.error('Error loading shipping costs. Please refresh the page and try again.');
                  } else if (this.isMounted()) {
                      // This change is not reflected in Row/Cell for this.state/props.percentages until after force a UI update (via handleAdd and handleCancel 
                      // where this.state.add changes) even though a) percentages (a single object) holds valid values here and does all the way down to &lt;Row /&gt;
                      // and b) there is no similar issue with this.state.data.
                      this.setState({ data: data.Value.ShippingCostMatrix, percentages: data.Value.ShippingPercentage });
                  }
              }.bind(this));
          },
          componentDidMount: function() {
              this.loadFromServer();
          },
          handleCellChange: function(rowIdx, prop, val) {
              var row = copy(this.state.data[rowIdx]);
              row[prop] = val;
              var rows = this.state.data.slice();
              rows[rowIdx] = row;
              rows.sort(sortBySubtotal);
              this.setState({data: rows});
          },
          handlePercentCellChange: function(prop, val) {
              var row = copy(this.state.percentages);
              row[prop] = val;
              this.setState({percentages: row});
          },
          handleAdd: function(event) {
              event.preventDefault();
              this.setState({ add: true});
          },
          handleAddCancel: function(event) {
              event.preventDefault();
              this.setState({ add: false});
          },
          render: function() {
              var ctrl;
              if (this.state.add) {
                  ctrl = (&lt;NewRow onAddCancel={this.handleAddCancel} onRowAdd={this.handleRowAdd} /&gt;);
              }
              else {
                  ctrl = (
                      &lt;div&gt;
                      &lt;p&gt;&lt;a onClick={this.handleAdd} className="btn btn-primary btn-lg"&gt;Add&lt;/a&gt;&lt;/p&gt;
                      &lt;Grid data={this.state.data} percentages={this.state.percentages}
                           onCellChange={this.handleCellChange} onPercentCellChange={this.handlePercentCellChange} onRowCommit={this.handleRowCommit}  onPercentRowCommit={this.handlePercentRowCommit} onRowDelete={this.handleRowDelete} /&gt;
                      &lt;/div&gt;
                   );
              }
  
              return &lt;div&gt;{ctrl}&lt;/div&gt;;
          }
      });
  
      //React.render(&lt;ShippingTable initialData={ {data : [], percentages: { Canada: 1, Us: 1.25, International: 2.25, Id: 1 }, add: false} } 
      React.render(&lt;ShippingTable initialData={ {data : [], percentages : {}, add: false} } 
          url="/admin/shipping/costs" update="/admin/shipping/update" create="/admin/shipping/create" delete="/admin/shipping/delete" updatePercentage="/admin/shipping/updatepercentage" /&gt;, document.getElementById('shipTable'));
  </code></pre>