Skip to content

Commit

Permalink
Automatically restart the game for new cards
Browse files Browse the repository at this point in the history
If new cards are passed as a prop, automatically restart the game with the new cards.
  • Loading branch information
guioconnor committed Mar 9, 2018
1 parent 41a7b32 commit cc3baa4
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/module/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,17 @@ export class MemoryGame extends Component {
}
}

componentWillReceiveProps(nextProps) {
if (nextProps.cards !== this.props.cards) {
this.resetGame(nextProps.cards);
}
}

getChildContext() {
return {
[MEMORY_GAME_CONTEXT]: {
cards: this.getMemoryCards(),
resetGame: this.resetGame,
resetGame: this.resetGame.bind(this, this.props.cards),
movesCounter: this.state.moves,
pairsFoundCounter: this.state.pairsFound,
},
Expand Down Expand Up @@ -117,9 +123,9 @@ export class MemoryGame extends Component {
};
};

resetGame = () => {
resetGame = cards => {
clearTimeout(this.timeout);
this.setState(this.getInitialState(this.props.cards), () => {
this.setState(this.getInitialState(cards), () => {
if (this.props.glimpse) {
this.startGlimpse();
}
Expand Down

0 comments on commit cc3baa4

Please sign in to comment.