I have read:
ReactJs: How to pass the initial state while rendering a component?
ReactJS: Why is passing the component initial state a prop an anti-pattern?
Am still having trouble resolving this issue. Namely - rendering an array of child component declarations with props that rely on the initial state of the parent component.
I am new to reactjs and somewhat new to development. Any help would be appreciated.
boardCreation: function() {
var board = [];
for (var i = 0; i < 6; i+=1) {
board.push(<Card key={i} onClick={this.onCardFlip} image={this.state.imagesArray[i]} flipped={this.flipped[i]} cardIndex={i} />);
};
return board;
},
getInitialState: function() {
return {
imagesArray: this.shuffledImages(['walle.jpg', 'walle.jpg', 'eve.jpg', 'john.jpg', 'captain2.jpg', 'mary.jpg']),
flipped: [false, false, false, false, false, false],
flippedImages: [],
walleCount: 0,
board: this.boardCreation(),
}
},
The above returns 'Uncaught TypeError: Cannot read property 'imagesArray' of null'.
Initially I had the board creation in the render function of this parent component - however the board and all other states will need to reset every time a game ends and a new one begins.
Thank you.