Created
April 27, 2017 17:28
-
-
Save GiancarlosIO/7372e70d932028b64fbf0127dae17fd8 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
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