In this post join me as I create a React Custom Hook which will encapsulate the logic behind a simple Pagination component.
A Pagination component...
For further actions, you may consider blocking this person and/or reporting abuse
Nice tutorial 👏.
Small bug in the implementation, this is the test to find it
Hey, thanks for the feedback :)
I might have changed the implementation since then, but I will have a look.
Thanks!
I really liked the approach of this article using TDD and also enjoyed working with the
react-hooks
helpers. I referenced a bunch of various pagination tutorials and gists on the web and even old stuff I'd written a while back to find a solution I liked. I followed along mostly similar to what you have here but then completely tore down the entire design. I realized that, in my design,usePagination
hook only needed to take care of generating the pagination links and nothing else. Totally SRP cohesive. Then, the React component would deal with pretty much only rendering the pagination controls. The consumer would, in fact, take care of theconst [currentPage, setCurrentPage]
state and simply listen foronPageChanged
callback, update the current page, then have a listener inuseEffect
that would regenerate the paging links based off of the updated current page. There's a lot of code between the three, but I can show the tests for myusePagination
as I think you'll find it interesting that it's basically a completely different approach!Here are the relevant scripts if you'd like to see the full approach: Pagination.tsx, usePagination.ts, Storybook consumer story.
A couple of things I'd note:
Thanks for the kind words @roblevintennis :)
Surely there are better ways to implement a Pagination React hook than what I did, and yours does looks like a more robust one. My goal in the article was to focus on the TDD aspect of developing such custom component, so it was never meant to be a complete Pagination solution :)
Having said that, I am not sure how I feel about the Hook supplying the array of links... In my case the hook is much "dumber" in the sense that I leave the buffering (and in your case, the offset ) to be something that the consuming component should take care of.
As for the cyclic nature of the pagination, you're right, this is more of a carousel kinda feature, but heck, why not? ;)
In any case, thanks for sharing your approach!
Gotcha, yeah, there are probably many viable approached — I'd say again that your tutorial on setting up the TDD test bed really really helped and was quite valuable! Not sure if you've ever read Kent Beck's TDD by example? But I have had a long love/hate with unit tests but one case where I feel they shine is when you feel a bit uncertain as to how to go about something and, for me, implementing pagination is quite a challenge indeed!
Good stuff…the dev world is definitely better when we share these ideas! Thanks :)
Cannot agree more :)
Great tutorial! Thank you!
Instead of using
useReducer
for such a simple use case, you could create a new function that would check the new cursor value before calling hook'ssetCursor
:...alrhough this also looks complicated so I'm not sure what's better.
I'd also rename
cursor
topage
as I'd reservecursor
for GraphQL paginationcursor
which is usually not a number but a string.Sorry for formatting, I'm typing this on my phone w/ autocorrect.
Wow, I replay this tutorial =) Thx.
Great tutorial! Super understandable!!
Great stuff! Tutorials are so much easier to follow when you use a TDD cycle. Divide and conquer simply works.
Great tutor and with TDD, it is excellent. Hope to see more tutors with TDD approach
We need more testing content. Tnx great post.
Thansk! Is there any testing content you find lacking in particular?