title: Listening for models inside reactjs components
body: <p>I'm currently trying out reactjs, trying to make a MVC based card game with it. I find that ReactJS is a very capable and good performing view library, but I have some troubles mapping it to a more traditional MVC pattern where the views listen to the models.</p>
  
  <p>In the card game I have 2 rows of cards on the table, one of the current player and one of the opponent player. The cards of the current player are in the bottom row and they are selectable. In my GameView i render 2 child views, like this:</p>
  
  <p><code>
  &lt;SlotView key="slotview1" slot={this.state.otherPlayer.slot} selectable={false} /&gt;
  &lt;SlotView key="slotview2" slot={this.state.currentPlayer.slot} selectable={this.state.playing_cards} on_select={this.handlePlayCard}&gt;
  </code></p>
  
  <p>when the turn is over I swap the current player and the other player (in the game model) and notify the gameview via an event. The gameview's state is updated, and then it rerenders itself from the perspective of the other player. </p>
  
  <p>This works fine, the cards of the other player show in the bottom row. But, the problem I am running into is that the SlotView (child view of the game view) also listens to its model (it updates itself when a card is played). </p>
  
  <p>I bind the listeners in SlotView#ComponentDidMount and unbind them in SlotView#ComponentDidUnmount. However, the component is only mounted during the first render, and never unmounted and mounted again. Its property (slot) is being updated to the other player, but it still listens to events from the first player. Is there a way I can listen to property changes to bind/unbind event handlers? Or should I not listen to models at all in child components and replace the MVC model with a more push oriented model (but this seems to produce a lot of boilerplate code with nested views)?
  Should I unbind/rebind in the render method itself (but that seems wrong too)?</p>
  
  <p>Curious to hear how other people do this....</p>