Created
June 27, 2018 02:43
-
-
Save JonathanLoscalzo/c8bfb12e269b809a9709d37c62f5c7a6 to your computer and use it in GitHub Desktop.
filterable input - react
This file contains hidden or 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'; | |
| class App extends React.Component { | |
| constructor(){ | |
| super(); | |
| this.state = {items: []} | |
| } | |
| componentWillMount(){ | |
| fetch( 'https://swapi.co/api/people/?format=json' ) | |
| .then( response => response.json() ) | |
| .then( ({results: items}) => this.setState({items})) | |
| } | |
| filter(e){ | |
| this.setState({filter: e.target.value}) | |
| } | |
| render(){ | |
| let items = this.state.items; | |
| if(this.state.filter){ | |
| items = items.filter( item => | |
| item.name.toLowerCase() | |
| .includes(this.state.filter.toLowerCase())) | |
| } | |
| return ( | |
| <div> | |
| <input type="text" | |
| onChange={this.filter.bind(this)}/> | |
| {items.map(item => | |
| <Person key={item.name} person={item} />)} | |
| </div> | |
| ) | |
| } | |
| } | |
| const Person = (props) => <h4>{props.person.name}</h4> | |
| export default App |
This file contains hidden or 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'; | |
| import ReactDOM from 'react-dom'; | |
| import App from './App'; | |
| ReactDOM.render(<App />, document.getElementById('app')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment