Skip to content

Instantly share code, notes, and snippets.

@GiancarlosIO
Created April 27, 2017 17:28
Show Gist options
  • Save GiancarlosIO/7372e70d932028b64fbf0127dae17fd8 to your computer and use it in GitHub Desktop.
Save GiancarlosIO/7372e70d932028b64fbf0127dae17fd8 to your computer and use it in GitHub Desktop.
import React from 'react';
export default class App extends React.Component {
state: {
searchText: '',
data: [{id: 1, text: 'text 1'}, {id: 2, text: 'text 2'}]
}
renderFilteredData = () => {
const { searchText, data } = this.state
if (searchText) {
return data.filter(item => item.text.indexOf(searchText) > -1).map( item => (<li key={item.id}> {item.text} </li>) );
}
return data.map( item => (<li key={item.id}> {item.text} </li>) );
}
render() {
return (
<input value={this.state.text} onChange={(e)=>{this.setState({searchText: e.target.value })}}/>
<ul>
{this.renderFilteredData()}
</ul>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment