Skip to content

Instantly share code, notes, and snippets.

@JonathanLoscalzo
Created June 27, 2018 02:43
Show Gist options
  • Select an option

  • Save JonathanLoscalzo/c8bfb12e269b809a9709d37c62f5c7a6 to your computer and use it in GitHub Desktop.

Select an option

Save JonathanLoscalzo/c8bfb12e269b809a9709d37c62f5c7a6 to your computer and use it in GitHub Desktop.
filterable input - react
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
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