Using “React render props” to create a Paginated Lists.

Vince Llauderes
HackerNoon.com
3 min readSep 4, 2018

--

Having wondered how to use render props?

Great! This article will be fitted for you. I’ll be showing you how to create a Paginated Lists together with the React render props.

React render props allow us to encapsulate the data that we want to share to other components. With this design it will promotes a highly reusable components in your Application.

This is our finished PaginatedList Component and I’ll explain here how I achieve making this code.

In our PaginatedLists Component we have a three props. The first prop is for lists of items that will become paginated later. The second prop is the itemsPerPage which will determine how many items will be displayed per page. The third prop is what we called render prop, in this prop you accepts a function that return another component component. Later Everytime we click the page number this prop will fired and receive a callback function which passed the newly updated paginated list and render the component inside it.

The render prop can be named anything you want. We named it render because it was a convention in ReactJS.

Inside our PaginatedLists Component we have a function paginatedLists which will make our lists become paginated by using the JavaScript built-in slice function.

The onClickPageNumber update our currentPage state and also our lists everytime the we click the page number.

In our render method inside the return function you may notice the this.props.render(this.paginatedLists(lists, itemsPerPage)) and calling the paginatedLists function to paginate the lists. This code will call the render prop that we saw earlier when we call the Component <Paginated Lists /> that everytime we update the currentPage state this line of code will fire and the render prop method will call also and rerender the component inside it.

Output of our PaginatedLists Component.

Now You can use this PaginatedLists Component through out in your application to make the lists of items become paginated.

If you enjoy reading this article give me a clapped…

Hope it Helps ^_^

Thanks.

“Don’t be a JavaScript Mediocre.”

Follow me on twitter https://twitter.com/llaudevc/

--

--