Last active
November 12, 2015 00:15
-
-
Save Teino1978-Corp/98d314bf87da430424c3 to your computer and use it in GitHub Desktop.
http://stackoverflow.com/questions/25331732/reactjs-have-a-main-component-that-doesnt-own-its-children/25335645#25335645; A turn-based, strategy game in JavaScript http://games.lqbs.fr/reach/; https://github.com/AdrianGaudebert/reach/blob/master/js/player.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EventEmitter = require('events').EventEmitter; | |
var fooEvents = new EventEmitter(); | |
var Counter = React.createClass(){ | |
mixins: [makeChangeEventMixin(fooEvents)], | |
getInitialState: function(){ return {count: 0} }, | |
render: function(){return <div onClick={this.handleClick}>{this.state.count}</div>}, | |
handleClick: function(){ | |
this.emitStateChange({count: this.state.count + 1}); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var makeChangeEventMixin = function(emitter){ | |
return { | |
componentDidMount: function(){ | |
emitter.addListener('change', this.__makeChangeEventMixin_handler_); | |
}, | |
componentWillUnmount: function(){ | |
emitter.removeListener('change', this.__makeChangeEventMixin_handler_); | |
}, | |
__makeChangeEventMixin_handler_: function(payload){ | |
this.setState(payload); | |
}, | |
emitStateChange: function(payload){ | |
emitter.emit('change', payload); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment