Skip to content

Instantly share code, notes, and snippets.

@rahmanusta
Created February 26, 2016 14:43
Show Gist options
  • Save rahmanusta/0fac61a3f76982233030 to your computer and use it in GitHub Desktop.
Save rahmanusta/0fac61a3f76982233030 to your computer and use it in GitHub Desktop.
React Dynamic List Adder
var id = 0;
var NameRow = React.createClass({
render: function () {
return (<div>
<ul>
{this.props.names.map(function (e) {
return (<li key={e.id}>{e.name}</li>);
})}
</ul>
</div>);
}
});
var NameInput = React.createClass({
onAddName: function () {
this.props.onAddName(this.refs.nameInput.value);
},
render: function () {
return (<div>
<input ref="nameInput" type="text"/>
<button onClick={this.onAddName}>Ekle</button>
</div>);
}
});
var NameContainer = React.createClass({
getInitialState: function () {
return {
names: this.props.names || []
};
},
onAddName: function (name) {
var clonedNames = this.state.names.slice(0);
clonedNames.push({
id: id++,
name
});
this.setState({
names: clonedNames
});
},
render: function () {
return (<div>
<NameInput onAddName={this.onAddName}/>
<NameRow names={this.state.names}/>
</div>);
}
});
var NAMES = [
{
id: id++,
name: "Rahman"
},
{
id: id++,
name: "Hakan"
},
{
id: id++,
name: "Ayşe"
},
];
ReactDOM.render(<NameContainer names={NAMES}/>, document.querySelector("#root"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment