A Junior approach to react-pagination with Laravel 9.
Tags: beginners, laravel, react, codenewbie
Published: 2022-10-19 09:22:10

_Cover photo by Tim Wildsmith on Unsplash_


![snapshot of footer section of website showing round buttons navigation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xixndwapir6nng9la3qe.png)
    As I write this, I'm quite new to programming. I only have 7  months of an intensive bootcamp for Full stack web developer on my back.
I'm still on training wheels, and my biggest resources are google,  Dev.to and Stack overflow.
I hear from senior developer friends that those never really come off. 
> _"Most of us can't code from memory only, there is always web search involved somewhere along the process."_  - they say.
> _"Focus on learning the basic logic structures of the languages and frameworks, the rest is just syntax."_  - they advised me.

>Well then! That sounds simple enough...😒

  In theory it is simple. Well all programming languages have the same basic concepts for data structure, control structure and sequencing, right?
Here is a [very nice article](https://www.codelicious.com/blog/concepts-in-every-coding-language) on this subject.


But when you jump from language to language, framework to framework, it can become quite confusing. Everything is still so raw in your "junior" memory that you just mix everything.

## Then came a Laravel/React social platform project

  So when I had to work on the pagination feature for a social platform project made with Laravel and React, I felt overwhelmed. I had had only one week of React/Laravel experience.

>_Well the whole social platform project was overwhelming but I will only focus on this simple feature for this article... right..._ 🙃 _let's keep going shall we?_ 

I remembered the advice I was given, and began this saga with the usual google search.
>_Let me see how others did it?_ 🤔.

![snapshot of google search input field with sentence "Laravel React pagination](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h2mj0j66mcbb1t90nxup.png)

  After long stretches of production time spent on endless react components and youtube video tutorials and /r/AskReddit or /r/HoldMyBeer... 

>_Wait! What?_😐
 _... how did I get there...?_ 
_errmmm..._ 😁 

  I was more confused than before. 
If I got it right, I would have to create a component file inside my MVC folder for components, that would be full with logic that seemed complicated math stuff.

> _My skills at math don't go beyond the simple calculator any windows or android or mac OP system has, it's embarrassing._😬

And then would have to import this component to any page I wanted to use it in.

## Right! okay, what was that advice again...? 😕

  I had done a very easy pagination before. In a simple Laravel/Breeze/Tailwind project. Where all I had to do, was to call the in built method "paginate" on the query builder and then add a line of code on the blade page and it worked perfectly! 

_Query builder_
```
class UserController extends Controller
{
    /**
     * Show all application users.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('user.index', [
            'users' => DB::table('users')->paginate(15)
        ]);
    }
}
```

---
_blade page_

```
<div class="container">
    @foreach ($users as $user)
        {{ $user->name }}
    @endforeach
</div>
 
{{ $users->links() }}
```

 You can learn more on that here:
[Laravel pagination doc](https://laravel.com/docs/9.x/pagination)


  Alright, but for this project all the front end was built with React, so I could not use this. On the other hand for sure I was not the only developer in the world that had to make a pagination with React. For sure there was a similar package or plug-in, something of this sort that would make all developers life a bit easier when it came to pagination. 

>This is something I❣️ in the developers & IT world community! It's the fact that everyone shares knowledge! 
There is this in-built explorer spirit that loves to go out there into the unknown, like the sea explorers of the 14th century, and battle those sea monsters, overcome challenges, to find new perspectives and more efficient solutions, not to reinvent the wheel but to build on it and grow. Because who knows where it will takes us? What new and wonderful worlds we can build?  And one thing is for sure if 2 heads think better than one, **[26.9 million](https://truelist.co/blog/software-development-statistics/#:~:text=2.,has%20over%206%20million%20developers.)** of us 🤯 will think much better!

Therefor I was certain that I wouldn't need to build a pagination from scratch. Maybe there was a similar package approach or built in component that would do the job?
So, after a bit more searching I finally found what I wanted.

[Npm React-paginate doc](https://www.npmjs.com/package/react-paginate)

I just had to download the package and add a bit more lines of code than with the Laravel solution, but most of the hard work that was difficult for my newbie skills was handled by the package.

The Laravel-react project was already installed and the authentication, database tables, migrations and page routing was all being handled by Laravel & Breeze in the back end (my group colleagues where developing that part). So I could pull up my sleeves and jump directly to work on building the page _view_ for the section o the platform I was responsible for and then adding the pagination feature.



## Let's do this!



I began by installing the package:

```php
npm install react-paginate --save
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kost58a0fb1h13zkwt0f.png)

   After building a basic html _view_, adding all components like the "posts" card and placing all the elements from the array I wanted to "map" and properly display the "Post", I felt I had enough of a foundation to bring in the pagination. 

```php
import { Head } from "@inertiajs/inertia-react";
import CentralLogo from "@/Components/CentralLogo";
import DarkBlueBlockHeader from "@/Components/DarkBlueBlockHeader";
import PostCard from "@/Components/PostCard";

export default function Profile(props) {
    return (
        <>
            <Head title="Profile" />

            <div className="relative w-full h-fit flex flex-col overflow-auto items-center">
                <DarkBlueBlockHeader className="">
                    <section className="flex-row">
                        <h1 className="font-bold text-gray-100 text-5xl m-4 p-2">
                            Hello {props.auth.user.name}
                        </h1>
                    </section>
                    <CentralLogo
                        width="100px"
                        src="images/icon/waldo.png"
                        alt="user avater image"
                    />

                    <h1 className="text-center text-gray-400 text-2xl m-4 p-2">
                        Lost in posts?
                        <br />
                        This page contains them all.
                    </h1>
                </DarkBlueBlockHeader>
            </div>

            <div className="flex flex-col max-w-full sm:pt-10">
                <section className="absolute right-0 mr-32">
                    <SearchBar />
                </section>

                <div className="flex flex-col justify-center items-center sm:items-center sm:pt-10">
                    <section className="mb-6">
                        <h1 className="font-bold text-2xl">My Posts</h1>

                        {posts.map(
                            ({ id, title, content, tag, page, user }) => {
                                return (
                                    <PostCard key={id}>
                                        <h1 className="mt-1 font-bold text-gray-700">
                                            {title}
                                        </h1>

                                        <p className="ml-2 mb-4 text-gray-700">
                                            <b>by {user.name}</b>
                                        </p>

                                        <div
                                            id="postContent"
                                            type="textfield"
                                            className="flex flex-col ml-2 mb-5 w-[500px] text-ellipsis "
                                        >
                                            {content}
                                        </div>
                                        <p className="ml-2 mb-1 text-lg font-bold text-gray-600">
                                            <b className="text-lightBlue">
                                                From: {page}
                                            </b>
                                        </p>

                                        <p className="ml-2 mb-1 font-small text-gray-600">
                                            <b className="text-lightBlue">
                                                tags: {tag}
                                            </b>
                                        </p>
                                    </PostCard>
                                );
                            }
                        )}
                    </section>
                </div>
            </div>

            <div className="h-64">
                <Footer></Footer>
            </div>
        </>
    );
}

```

But off course I would then have to adapt the npm tutorial logic to my logic.
Now comes the tricky part.
So I broke it down in simple steps:

#### First: 

Make use of the React hook _useState_ to set the initial state of the array of items. The target here was the array of posts being created by the platform users so I called this variable _"posts"_.
It also seemed logic then that I would pass this array being built and used in other "corners of this platform", along in my function as a _props_ argument _props.posts_.

#### Second: 

Do the same for the initial state of the page displaying the posts. The variable I called _"page"_ and set it to 0.

#### Third:

Create the logic flow:
   - Posts per page displayed  will be 3;
   - Number of posts visited will be equal to the page visited multiplied by the number of posts displayed per page.
   - Then in order to keep track of the total number of pages visited and keep moving forward in the array, I have to divide the total _"length of the posts array"_ by the number of _"post per page"_ and round that result to an integer using the method _Math.ceil()_.
   - Finally I need to set an event listener function or in this case a "_onPageChange_" method in order to make all this logic happen when the user clicks the _next or previous_ page button.


And here is the overview of all of this pagination logic:


```php
    //// pagination ////

    const [posts] = useState(props.posts);
    const [page, setPage] = useState(0);
    const postsPerPage = 3;
    const numberOfPostsVisited = page * postsPerPage;
    const totalPages = Math.ceil(posts.length / postsPerPage);
    const changePage = ({ selected }) => {
        setPage(selected);
    };

    //// pagination ////
```


  Right! The flow was set but there was still some pieces of this pagination recipe puzzle missing.
The array of data still needed to be correctly distributed (_sliced_) trough the navigation pages, and I also needed to assign all the props properties from this package component to the correct variables that will make this logic recipe _"bake the pagination cake_".


###Fourth

Now to implement the rest of the ingredients and cook this batter:

```php

<section className="mb-6">
    <h1 className="font-bold text-2xl">My Posts</h1>

    {posts
        .slice(numberOfPostsVisited, numberOfPostsVisited + postsPerPage)
        .map(({ id, title, content, tag, page, user }) => {
            return (
                <PostCard key={id}>
                    <h1 className="mt-1 font-bold text-gray-700">{title}</h1>

                    <p className="ml-2 mb-4 text-gray-700">
                        <b>by {user.name}</b>
                    </p>

                    <div
                        id="postContent"
                        type="textfield"
                        className="flex flex-col ml-2 mb-5 w-[500px] text-ellipsis "
                    >
                        {content}
                    </div>
                    <p className="ml-2 mb-1 text-lg font-bold text-gray-600">
                        <b className="text-lightBlue">From: {page}</b>
                    </p>

                    <p className="ml-2 mb-1 font-small text-gray-600">
                        <b className="text-lightBlue">tags: {tag}</b>
                    </p>
                </PostCard>
            );
        })}

    <section className="mx-4 my-4">
        <ReactPaginate
            pageRangeDisplayed={5} //The range of buttons pages displayed.
            previousLabel={"Previous"} //lable for previous page button
            nextLabel={"Next"} // lable for Next page button
            pageCount={totalPages} // place here the variable for total number of pages
            onPageChange={changePage} // place here the trigger event function
            /// navigation CSS styling ///
            containerClassName={"navigationButtons"}
            previousLinkClassName={"previousButton"}
            nextLinkClassName={"nextButton"}
            disabledClassName={"navigationDisabled"}
            activeClassName={"navigationActive"}
            /// end navigation styling ///
        />
    </section>
</section>;

```


## My last thoughts


   My simple naïve approach was far from good but it got the work done. I was not able to make it work like a separate component due to my lack of experience with React and had therefor to implement this "recipe" in every page we had a need for pagination, but here comes the beauty of development, even with only one week of experience with React and Laravel I was able to make a functional pagination feature for my Social Platform project.
I was thrilled with my newbie result. :)

A final overview!

```php
import React, { useState, useEffect } from "react";
import { Head } from "@inertiajs/inertia-react";
import CentralLogo from "@/Components/CentralLogo";
import DarkBlueBlockHeader from "@/Components/DarkBlueBlockHeader";
import PostCard from "@/Components/PostCard";

export default function Profile(props) {

    /// The useState attributed to the posts array is being used to set intial sate for the pagination to use the array of posts brought in by the global variable props ////
    const [posts] = useState(props.posts);

 
    //// pagination ////
    const [page, setPage] = useState(0);
    const postsPerPage = 3;
    const numberOfPostsVisited = page * postsPerPage;
    const totalPages = Math.ceil(posts.length / postsPerPage);
    const changePage = ({ selected }) => {
        setPage(selected);
    };
    //// pagination ////

    return (
        <>
            <Head title="Profile" />

            <div className="relative w-full h-fit flex flex-col overflow-auto items-center">
                <DarkBlueBlockHeader className="">
                    <section className="flex-row">
                        <h1 className="font-bold text-gray-100 text-5xl m-4 p-2">
                            Hello {props.auth.user.name}
                        </h1>
                    </section>
                    <CentralLogo
                        width="100px"
                        src="images/icon/waldo.png"
                        alt="user avater image"
                    />

                    <h1 className="text-center text-gray-400 text-2xl m-4 p-2">
                        Lost in posts?
                        <br/>This page contains them all.
                    </h1>
                </DarkBlueBlockHeader>
            </div>

            <div className="flex flex-col max-w-full sm:pt-10">
                <section className="absolute right-0 mr-32">
                    <SearchBar />
                </section>

                <div className="flex flex-col justify-center items-center sm:items-center sm:pt-10">
                <section className="mb-6">
    <h1 className="font-bold text-2xl">My Posts</h1>

    {posts
        .slice(numberOfPostsVisited, numberOfPostsVisited + postsPerPage)
        .map(({ id, title, content, tag, page, user }) => {
            return (
                <PostCard key={id}>
                    <h1 className="mt-1 font-bold text-gray-700">{title}</h1>

                    <p className="ml-2 mb-4 text-gray-700">
                        <b>by {user.name}</b>
                    </p>

                    <div
                        id="postContent"
                        type="textfield"
                        className="flex flex-col ml-2 mb-5 w-[500px] text-ellipsis "
                    >
                        {content}
                    </div>
                    <p className="ml-2 mb-1 text-lg font-bold text-gray-600">
                        <b className="text-lightBlue">From: {page}</b>
                    </p>

                    <p className="ml-2 mb-1 font-small text-gray-600">
                        <b className="text-lightBlue">tags: {tag}</b>
                    </p>
                </PostCard>
            );
        })}

    <section className="mx-4 my-4">
        <ReactPaginate
            pageRangeDisplayed={5} //The range of buttons pages displayed.
            previousLabel={"Previous"} //lable for previous page button
            nextLabel={"Next"} // lable for Next page button
            pageCount={totalPages} // place here the variable for total number of pages
            onPageChange={changePage} // trigger event function
            /// navigation styling ///
            containerClassName={"navigationButtons"}
            previousLinkClassName={"previousButton"}
            nextLinkClassName={"nextButton"}
            disabledClassName={"navigationDisabled"}
            activeClassName={"navigationActive"}
            /// end navigation styling ///
        />
    </section>
</section>;


                </div>
            </div>

            <div className="h-64">
                <Footer></Footer>
            </div>
        </>
    );
}


```