I'm creating a minesweeper React app and inside the Board stateful component I've initialized and populated the board with mines and other information.
The initialization of this board is made as follows
// Method that creates a board of empty tiles, populates it with mines and gets the number of neighbouring
// mines for every tile on the board. Returns the initialized board  
initializeBoard(height, width, mines){
    const emptyTiles = this.createEmptyArray(height, width);
    const populatedTiles = this.populateBoard(emptyTiles, height, width, mines);
    const neighbourTiles = this.getNumOfMinesNeighbours(populatedTiles,height, width);
    return this.renderBoard(neighbourTiles);
   }
renderBoard returns a JSX component.
I receive height, width and mines as props passed from another component.
My Board status looks like this:
state = {
    mineCount: this.props.mines,
    gameStatus: null,
    boardData: null
}
And I want to dynamically render the initialized board and update Board's state value for boardData key. Now, I've arrived two different approaches for this:
- dynamically call initialize board() in Board's render() method, and update its state somehow
- Directly assign the state boardData value with a call to initializeBoard()
Approach #1 would look like this, inside render():
render() {  
    const board= this.initializeBoard(this.props.height, this.props.width, this.props.mines);
    //Save state throws Maximum update depth exceeded.
    return (
        <div>
            {board}
        </div> ...} //end of render
Approach #2:
state = {
    mineCount: this.props.mines,
    gameStatus: null,
    boardData: this.initializeBoard(this.props.height, this.props.width, this.props.mineCount)
}
Now, I know that setting the state of the component inside render() is a no-no, but also I am unable to find a proper lifecycle hook that will work when an object is created prior to render() and then dynamically rendered into JSX, since I'm not receiving this object via props.
So What I want to know is this:
- Is the approach #2 a good/appropriate one? Or is calling a method to store a state's key value a bad practice?
- Is there something I'm missing when it comes to lifecycle hooks and objects created prior to render()?
- Is there any 'best practice' to store a state's key value in this situation?
 
     
     
    