How to create load more logic in React
Tags: react, html, javascript, css
Published: 2020-07-04 02:58:54


![How to create load more logic in React](https://i1.wp.com/blogreact.com/wp-content/uploads/2020/07/ajax-load-more-banner.png?w=750&ssl=1)

So usually we prefer to use load more logic using the backend api where we pass the skip value and it will return us the next set of data but what if we want to implement same logic in frontend for some specific cases. In this article we will gonna learn how to create load more logic in React.

Checkout  [this](https://blogreact.com/pagination/)  better way of pagination in backend.

## Problem

Let’s assume we have an Array of 100 objects in frontend and we want to display only n items at a time and next set of data will be loaded on l**oad more**  button click.

We usually do that thing using the backend where we passes the skip value and it will return us the next set of data but now we want to do same thing in our React Frontend.

## Solution

_**First of all we will gonna have few variable for our load more logic.**_

The length of our data is specified using the  **LENGTH**.

```
const LENGTH = 50;
```

After that I created an Array  **DATA**  of length  **LENGTH**.

```
const DATA = [ ...Array(LENGTH).keys() ];
```

Then a random image for showing in UI i.e  **IMAGE_SRC**.

```
const IMAGE_SRC="https://source.unsplash.com/random";
```

Limit will be 10. i.e  **LIMIT**

```
const LIMIT = 10;
```

```
const LENGTH = 50;
const DATA = [ ...Array(LENGTH).keys() ];
const IMAGE_SRC="https://source.unsplash.com/random";
const LIMIT = 10;
```

_**Now we will gonna write few states.**_

We also gonna use  **slice**  and  **concat**  from lodash for simplicity.

To check we have more records or not.

```
const [showMore,setShowMore] = useState(true);
```

Initial render list

```
const [list,setList] = useState(slice(DATA, 0, LIMIT));
```

For managing the index.

```
const [index,setIndex] = useState(LIMIT);
```

Our load more logic will gonna do the following things.

-   It will update the index by adding LIMIT to it.
-   Check for we have more records or not.
-   Gonna update our list by adding next set to our Array.

```
  const loadMore = () =>{
    const newIndex = index + LIMIT;
    const newShowMore = newIndex < (LENGTH - 1);
    const newList = concat(list, slice(DATA, index, newIndex));
    setIndex(newIndex);
    setList(newList);
    setShowMore(newShowMore);
  }
```

Our Render method will gonna return list of images and a load more button.

```
   <div className="App">
      <div className="image-container">
      {list.map(()=><img src={IMAGE_SRC} alt="random"/>)}
      </div>
      {showMore && <button onClick={loadMore}> Load More </button>}
    </div>
```

Our full component will gonna look like this.

```
import React,{useState} from "react";
import {
  slice, concat, 
} from 'lodash';

import "./styles.css";

const LENGTH = 50;
const DATA = [ ...Array(LENGTH).keys() ];
const IMAGE_SRC="https://source.unsplash.com/random";
const LIMIT = 10;

export default function App() {
  const [showMore,setShowMore] = useState(true);
  const [list,setList] = useState(slice(DATA, 0, LIMIT));
  const [index,setIndex] = useState(LIMIT);

  const loadMore = () =>{
    const newIndex = index + LIMIT;
    const newShowMore = newIndex < (LENGTH - 1);
    const newList = concat(list, slice(DATA, index, newIndex));
    setIndex(newIndex);
    setList(newList);
    setShowMore(newShowMore);
  }
  return (
    <div className="App">
      <div className="image-container">
      {list.map(()=><img src={IMAGE_SRC} alt="random"/>)}
      </div>
      {showMore && <button onClick={loadMore}> Load More </button>}
    </div>
  );
}

```

You will gonna see output similar to this.

![](https://i2.wp.com/blogreact.com/wp-content/uploads/2020/07/image.png?fit=750%2C332&ssl=1)

Our React Frontend load more logic is ready.

[![Edit t32ld](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/great-bhaskara-t32ld?file=/src/styles.css)