diff --git a/CardStack.js b/CardStack.js index d7dafd7..d0392d3 100644 --- a/CardStack.js +++ b/CardStack.js @@ -121,7 +121,20 @@ export default class CardStack extends Component { } componentDidMount(){ + this.initDeck(); + } + componentWillReceiveProps(nextProps){ + if (nextProps.children !== this.props.children) { + this.setState({ + cards: nextProps.children, + cardA: nextProps.children[(this.state.topCard=='cardA')? this.state.sindex-2 : this.state.sindex-1], + cardB: nextProps.children[(this.state.topCard=='cardB')? this.state.sindex-2 : this.state.sindex-1] + }); + } + } + + initDeck() { // check if we only have 1 child if(typeof this.props.children !== 'undefined' && !Array.isArray(this.props.children)){ this.setState({ @@ -138,17 +151,6 @@ export default class CardStack extends Component { sindex: 2, }); } - - } - - componentWillReceiveProps(nextProps){ - if (nextProps.children !== this.props.children) { - this.setState({ - cards: nextProps.children, - cardA: nextProps.children[(this.state.topCard=='cardA')? this.state.sindex-2 : this.state.sindex-1], - cardB: nextProps.children[(this.state.topCard=='cardB')? this.state.sindex-2 : this.state.sindex-1] - }); - } } _resetCard(){