Skip to content

Instantly share code, notes, and snippets.

@mikaelbr
Created November 10, 2014 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikaelbr/5e5c562f05d8fb395880 to your computer and use it in GitHub Desktop.
Save mikaelbr/5e5c562f05d8fb395880 to your computer and use it in GitHub Desktop.
var TestList = React.createClass({
render: function() {
var i = 0;
var items = this.props.items.map(function(item) {
return (
<div key={i++}>{item.name}</div>
);
});
return (
<div>
<div>{items}</div>
<button onClick={this.props.addItem}>Legg til</button>
</div>
);
}
});
var TopItem = React.createClass({
render: function () {
return (
<h3>{this.props.item}</h3>
);
}
});
var TestForm = React.createClass({
getInitialState: function() {
return { items: [] };
},
addItem: function () {
// Don't mutate the item in state/props
this.setState({
items: ['Tore Lervik'].concat(this.state.items)
});
},
render: function() {
return (
<div>
<TestList items={this.state.items} addItem={this.addItem} />
<button onClick={this.showLog}>Vis logg</button>
<TopItem item={this.state.items[0]} />
</div>
);
},
showLog: function() {
console.log(this.state.items);
}
});
React.render(<TestForm/>, document.getElementById('example'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment