Skip to content

Commit cc3baa4

Browse files
committed
Automatically restart the game for new cards
If new cards are passed as a prop, automatically restart the game with the new cards.
1 parent 41a7b32 commit cc3baa4

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/module/index.jsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,17 @@ export class MemoryGame extends Component {
7676
}
7777
}
7878

79+
componentWillReceiveProps(nextProps) {
80+
if (nextProps.cards !== this.props.cards) {
81+
this.resetGame(nextProps.cards);
82+
}
83+
}
84+
7985
getChildContext() {
8086
return {
8187
[MEMORY_GAME_CONTEXT]: {
8288
cards: this.getMemoryCards(),
83-
resetGame: this.resetGame,
89+
resetGame: this.resetGame.bind(this, this.props.cards),
8490
movesCounter: this.state.moves,
8591
pairsFoundCounter: this.state.pairsFound,
8692
},
@@ -117,9 +123,9 @@ export class MemoryGame extends Component {
117123
};
118124
};
119125

120-
resetGame = () => {
126+
resetGame = cards => {
121127
clearTimeout(this.timeout);
122-
this.setState(this.getInitialState(this.props.cards), () => {
128+
this.setState(this.getInitialState(cards), () => {
123129
if (this.props.glimpse) {
124130
this.startGlimpse();
125131
}

0 commit comments

Comments
 (0)