Render <ItemsPage> component for routes "/", "/items", & "/items/:id"
<Route
path={["/items/:id", "/", "/items"]}
component={ItemsPage}
/>
Create <Pagination> component
const { globalState } = React.useContext(GlobalContext);
const { url } = globalState;
const [itemCount, setItemCount] = React.useState();
const perPage = 2;
const pageCount = Math.ceil(itemCount / perPage);
const getCount = async () => {
const response = await fetch(`${url}/items_count`);
const data = await response.json();
await setItemCount(data);
};
React.useEffect(() => {
getCount();
}, []);
<Link to={page > 1 && `/items/${page - 1}`}>
<Link to={page < pageCount && `/items/${page + 1}`}>
Add <Pagination> component to <ItemsPage>
const page = parseInt(props.match.params.id);
return (
<div>
<Pagination page={page || 1} />
<Items page={page || 1} />
</div>
);
Create <Items> component
const { globalState } = React.useContext(GlobalContext);
const { url } = globalState;
const [items, setItems] = React.useState([]);
make get request to API
const getItems = async () => {
const repsonse = await fetch(`${url}/items?page=${page}`);
const data = await repsonse.json();
setItems(data);
};
React.useEffect(() => {
getItems();
}, [page]);
{items.map((item) => (
<Item item={item} key={item.id} />
))}
Create API endpoint that returns a count of the items in the inventory
get "/items_count", to: "items#count"
Write count function
def count
@count = Item.count
render json: @count
end
Add 'will_paginate' to Gemfile
gem 'will_paginate', '~> 3.1.0'
Use 'will_paginate' in items index route to render a specific number of items per page
def index
@items = Item.paginate(page: params[:page], per_page: 2)
render json: @items
end