Skip to content

Instantly share code, notes, and snippets.

@codrin-iftimie
Created February 27, 2015 12:07
Show Gist options
  • Save codrin-iftimie/2e79c87030c8ec5e615d to your computer and use it in GitHub Desktop.
Save codrin-iftimie/2e79c87030c8ec5e615d to your computer and use it in GitHub Desktop.
//manager.js
var _ = require("lodash");
function StatusManager(){
this.componentsStatus = {};
}
_.extend(StatusManager.prototype, {
getStatus: function(identifier) {
return this.componentsStatus[identifier];
},
setStatus: function(identifier, newStatus) {
var currentStatus = this.componentsStatus[identifier] || {};
return _.extend(currentStatus, newStatus);
}
});
module.exports = new StatusManager();
//component.jsx
var React = require("react"),
stateManager = require("./manager");
module.exports = React.createClass({
getInitialState: function(){
return this.props.state;
},
render: function(){
return(
<div>
<button onClick={this.handleClick}>
Click me!
</button>
</div>
);
},
handleClick: function(){
stateManager.setStatus(this.props.identifier, {clicked: true});
}
});
//view.jsx
var React = require("react"),
stateManager = require("./manager");
module.exports = React.createClass({
render: function(){
return(
<div>
<Component identifier="comp1" state={stateManager.getStatus("comp1")}/>
<Component identifier="comp2" state={stateManager.getStatus("comp2")}/>
</div>
);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment