Skip to content

Instantly share code, notes, and snippets.

@dkblay
Last active August 6, 2019 15:54
Show Gist options
  • Save dkblay/08700e9563e45e9144debbcaca0c22ee to your computer and use it in GitHub Desktop.
Save dkblay/08700e9563e45e9144debbcaca0c22ee to your computer and use it in GitHub Desktop.
App
import React, { Component } from "react";
import { getGames } from "./api/api";
import Pagination from "./components/Pagination";
class App extends Component {
size = 12;
state = {
games: [],
totoal: null
};
componentDidMount() {
this.loadGames();
}
loadGames = async (page = 1) => {
try {
const { games, total } = await getGames({
size: this.size,
page
});
this.setState({ games, total });
} catch (e) {
console.error(e);
}
};
renderCards() {
const { games } = this.state;
if (!games.length) {
return <div>No Items Found...</div>;
}
return games.map(({ id, color }) => (
<div
key={id}
className="games__item"
style={{ backgroundColor: color }}
/>
));
}
render() {
const { total } = this.state;
return (
<div className="container">
<div className="games">{this.renderCards()}</div>
{!!total && (
<Pagination onClick={this.loadGames} total={total} size={this.size} />
)}
</div>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment