Created
May 3, 2019 02:52
-
-
Save joaopaulomoraes/e8bebb54089d87d572aeee74f6ce6621 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, { Component } from 'react' | |
import Avatar from './Avatar' | |
class Search extends Component { | |
state = { | |
query: '', | |
users: [] | |
} | |
handleSearch = event => { | |
const query = event.target.value | |
setTimeout(() => { | |
fetch(`https://api.github.com/search/users?q=${query}`) | |
.then(response => response.json()) | |
.then(users => { | |
this.setState({ | |
query, | |
users: users.items | |
}) | |
}) | |
}, 200) | |
} | |
render() { | |
const { query, users } = this.state | |
return ( | |
<div> | |
<input type="text" name="search" onChange={this.handleSearch} /> | |
<button>Search</button> | |
{query && <p>Results for: {query}</p>} | |
<div style={{ marginTop: 10 }}> | |
{users && | |
users.map(user => ( | |
<div key={user.id}> | |
<ul style={{ listStyle: 'none' }}> | |
<li>{user.login}</li> | |
<li> | |
<a href={user.url}>{user.url}</a> | |
</li> | |
<li> | |
<Avatar url={user.avatar_url} /> | |
</li> | |
</ul> | |
</div> | |
))} | |
</div> | |
</div> | |
) | |
} | |
} | |
export default Search |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment