Hacer cambios en API para buscar en el clínico en React
class App extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: false,
results: [],
error: null,
};
}
fetchData = (url, query, key) => {
fetch(`the/api/${url}${query}.json?api-key${key}`)
.then(res => res.ok && res.json())
.then(data => this.setState({ results: data.results, isLoading: false }))
.catch(error => this.setState({ error, isLoading: false }));
}
componentDidMount() {
fetchData('api_url', 'all', 'key_here');
}
handleCategory = (e) => {
const cat = e.target.getAttribute('data-facet');
fetchData('api_url', cat, 'key_here');
}
render() {
const { isLoading, results, error } = this.state;
if (isLoading) {
return <div className="loading-icon"></div>
}
if (error) {
return <div className="error"></div>
}
return (
<div className="wrapper">
<button data-facet="arts" onClick={this.handleCategory}>Arts</button>
{results.map(res => ...)}
</div>
);
}
}
Imoptimal