Created
March 17, 2015 08:34
-
-
Save pragyanatvade/31049d14898daab82299 to your computer and use it in GitHub Desktop.
React Unidirectional Data Flow
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 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