title: Run function to update state on component constructor (and later)
body: <p>I'm trying to write a little combat card game as a learning excercise to understand React and in particular, state. In the game I'd like each turn a new hand of cards to be drawn that can be played so I created a function that draws the cards and called it from the constructor:</p>
  
  <pre><code>class App extends Component {
      constructor() {
          super();
          this.state = {
              hero: {
                  str: 2,
                  currentStr: 2,
                  def: 2,
                  currentDef: 2,
                  health: 100,
                  currentHealth: 74,
                  moves: 3,
                  currentMoves: 3,
                  cards: [1, 1, 1, 2, 2, 2],
                  draw: 4,
                  hand: [],
              },
          };
  
          this.drawHand = this.drawHand.bind(this);
          this.drawHand();
      }
  
      drawHand() {
          let draw = [];
          let cards = this.state.hero.cards.slice();
          for (let i = 0; i &lt; this.state.hero.draw; i++) {
              const min = 0;
              const max = cards.length -1;
              const position = Math.floor(Math.random() * (max - min + 1)) + min;
              const item = cards.splice(position, 1);
              draw.push(item[0]);
          }
  
          draw = draw.sort();
  
          let hero = {...this.state.hero};
          hero.hand = draw;
  
          this.setState({
              hero: hero
          });
      }
  
      render() {
          return (
              &lt;div className="row"&gt;
                  {this.state.hero.hand.join(',')}
              &lt;/div&gt;
          );
      };
  };
  </code></pre>
  
  <p>When I create the App component however, I get the error </p>
  
  <pre><code>Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op.
  </code></pre>
  
  <p>I've tried reading around mounted/unmounted components but I can't seem to figure out what's going on here. I figure I need to call the <code>drawHand</code> method later in the rendering process when App is mounted - is there an event or similar I can use for this please?</p>