Last active
August 29, 2015 14:05
-
-
Save arnemart/c26aeddcb7d719b45529 to your computer and use it in GitHub Desktop.
Cursors example
This file contains 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 React = require('react'); | |
var Immutable = require('immutable-with-cursors'); | |
var mainComponent = React.createClass({ | |
getInitialState: function() { | |
return this.props; | |
}, | |
render: function() { | |
return React.DOM.ul( | |
null, | |
this.state.data.get('elements').map(function(element, index) { | |
var cursor = this.state.data.cursor(['elements', index], function(updated) { | |
this.setState({'data': updated}); | |
}.bind(this)); | |
return detailComponent({data: cursor}); | |
}.bind(this)).toArray() | |
); | |
} | |
}); | |
var detailComponent = React.createClass({ | |
handleClick: function() { | |
this.props.data.update('count', function(v) { return v + 1; }); | |
}, | |
render: function() { | |
return React.DOM.li( | |
null, | |
React.DOM.span(null, this.props.data.get('title')), | |
React.DOM.button( | |
{ onClick: this.handleClick }, | |
'Click me!' | |
), | |
this.props.data.get('count') | |
); | |
} | |
}); | |
var data = Immutable.fromJS({ | |
elements: [ | |
{ | |
title: 'Element one', | |
count: 0 | |
}, | |
{ | |
title: 'Element two', | |
count: 0 | |
}, | |
{ | |
title: 'Element three', | |
count: 0 | |
} | |
] | |
}); | |
React.renderComponent(mainComponent({data: data}), document.getElementById('main')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment