Skip to content

Instantly share code, notes, and snippets.

@pragyanatvade
Created March 17, 2015 08:34
Show Gist options
  • Save pragyanatvade/31049d14898daab82299 to your computer and use it in GitHub Desktop.
Save pragyanatvade/31049d14898daab82299 to your computer and use it in GitHub Desktop.
React Unidirectional Data Flow
var FilteredList = React.createClass({
filterList: function(event){
var updatedList = this.state.initialItems;
updatedList = updatedList.filter(function(item){
return item.toLowerCase().search(
event.target.value.toLowerCase()) !== -1;
});
this.setState({items: updatedList});
},
getInitialState: function(){
return {
initialItems: [
"Apples",
"Broccoli",
"Chicken",
"Duck",
"Eggs",
"Fish",
"Granola",
"Hash Browns"
],
items: []
}
},
componentWillMount: function(){
this.setState({items: this.state.initialItems})
},
render: function(){
return (
<div className="filter-list">
<input type="text" placeholder="Search" onChange={this.filterList}/>
<List items={this.state.items}/>
</div>
);
}
});
var List = React.createClass({
render: function(){
return (
<ul>
{
this.props.items.map(function(item) {
return <li key={item}>{item}</li>
})
}
</ul>
)
}
});
React.renderComponent(<FilteredList/>, document.getElementById('mount-point'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment