Created
November 10, 2014 12:30
-
-
Save mikaelbr/5e5c562f05d8fb395880 to your computer and use it in GitHub Desktop.
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 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