Paginacion component provides simple and intuitive navigation controls for paginating through lists of Pokemon. It renders “Previous” and “Next” buttons that emit events when clicked and automatically disables buttons when navigation is not available.
Component Code
Here’s the complete implementation of the Paginacion component:Paginacion.vue
Props
The component accepts two props to control button states:Controls whether the “Previous” button is enabled. When falsy, the previous button is disabled. Typically bound to the API’s
previous field indicating if there are previous results.Controls whether the “Next” button is enabled. When falsy, the next button is disabled. Typically bound to the API’s
next field indicating if there are more results.Events
Emitted when the “Anterior” (Previous) button is clicked. Parent component should handle this by decreasing the offset and fetching previous results.
Emitted when the “Siguiente” (Next) button is clicked. Parent component should handle this by increasing the offset and fetching next results.
Usage Example
Here’s how the Paginacion component is used in the PokemonsView:PokemonsView.vue
Integration with PokeAPI
The component is designed to work seamlessly with the PokeAPI pagination format:- The API returns
previousandnextURLs in the response - These URLs are passed to the component’s
inicioandfinprops - When truthy, buttons are enabled; when falsy (null), buttons are disabled
API Response Example
Behavior
Button States
Button States
Event Flow
Event Flow
- User clicks “Anterior” or “Siguiente” button
- Component emits
prevornextevent - Parent component handles event by updating offset
- Parent fetches new data from API
- New
inicioandfinvalues update button states
User Experience
User Experience
- Automatically scrolls to top when pagination changes
- Buttons are disabled during loading (handled by parent)
- Clear Spanish labels: “Anterior” (Previous) and “Siguiente” (Next)
Styling
The component renders unstyled buttons, allowing parent components to apply custom styles through CSS. The buttons are standard HTML button elements that can be targeted with CSS selectors.Best Practices
The component uses Vue 3 Composition API with
<script setup> for cleaner, more concise code.