title: Is React ref Order guaranteed?
body: <p>I'm constructing an element with an unknown number of children using React and I need a reference to each.  Consider the following code:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="true">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>class Items extends React.Component {
      constructor() {
          super();
          this.itemEls = [];
      }
  
      render() {
          return (
              &lt;div&gt;
                  {
                      this.props.items.map((item, index) =&gt; {
                          return &lt;div className="item" key={index} ref={(el) =&gt; this.itemEls.push(el)}&gt;{item}&lt;/div&gt;;
                      })
                  }
              &lt;/div&gt;
          );
      }
  
      componentDidMount() {
          console.log(this.itemEls.map((el) =&gt; el.innerHTML));
      }
  }
  
  ReactDOM.render(&lt;Items items={["Hello", "World", "Foo", "Bar"]} /&gt;, document.getElementById('container'));</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.item {
    display: inline-block;
    background: #EEE;
    border: 1px solid #DDD;
    color: #999;
    padding: 10px;
    font-family: sans-serif;
    font-weight: bold;
    margin: 10px;
    border-radius: 5px;
    cursor: default;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"&gt;&lt;/script&gt;
  
  &lt;div id="container"&gt;&lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I loop through the provided array and save a reference to each element as I go.  When the component mounts, I print out a list of the element contents in the order they were added to the array.  So far in all of my testing the order has been correct, but I can't find any documentation that confirms that this isn't just the most common result of a secret race condition going on in the background.</p>
  
  <p><strong>Is the order in which reference functions are called consistent?</strong></p>
  
  <p>The obvious solution here would be to pre-set the array length in <code>componentWillMount</code> and populate it with the <code>index</code> value from <code>this.props.items.map</code> - but for the sake of argument, let's say that's not possible in the actual situation I find myself in.</p>