title: Where to put `componentWillMount` initialisation function that modifies state?
body: <p>Since <code>componentWillMount</code> will be deprecated, I would like to know the recommended way of handling init functions that modify state. I'm aware the general approach regarding <code>componentWillMount</code> is to move things into either the constructor or <code>componentDidMount</code>, but there are problems with both in this case.</p>
  
  <p>Connect Four example code (non-essential code left out):</p>
  
  <pre><code>class App extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        rows: 6, // # rows of the grid; cols = rows + 1
        grid: [],
        fullColumns: [],
        // ...
      };
    }
  
    componentWillMount() {
      this.initGrid(this.state.rows);
    }
  
    initGrid(rows) {
      // Create grid data structure to keep track of which grid cells
      // contain checkers of which color:
      // 'grid' = array of COLUMN arrays!
      let grid = [];
      // Create the 'fullColumns' array to keep track of which grid columns are full:
      let fullColumns = [];
      let cols = rows + 1;
      for (let c = 0; c &lt; cols; c++) {
        fullColumns.push(false);
        let column = [];
        for (let r = 0; r &lt; rows; r++) {
          column.push(null);
        }
        grid.push(column);
      }
  
      this.setState({
        rows,
        grid,
        fullColumns,
      });
    }
  
    // ...
  
    render() {
      // ...
    }
  }
  </code></pre>
  
  <p>I cannot put the <code>this.initGrid</code> function in <code>componentDidMount</code> because <code>this.state.grid</code> needs to be initialised in order to mount the component.
  Neither can this function be called in the constructor, because it makes use of <code>setState</code>. The <code>initGrid</code> function is also used in other parts of the codebase, so it still needs <code>setState</code> in order to function there.</p>
  
  <p>Am I missing something obvious? What is the professional way to solve this?</p>