How to Load More Data on a Button Click Using React js

1 year ago admin Reactjs

In today's lesson, we will see how to load more data on a button click using react js, let's assume that we have an e-commerce website and we want to display only two products and the user can load more products on a button click, so let's see how we can do that. 


Load more data on a button click

So in the example below, we display only 2 products from the array, and once the user clicks on the button the method load more is triggered and the products to show are incremented by 2.

                                                    
                                                                                                                
import React,{useState} from 'react';

export default function ProductList() {
  const products = [
    {
        id: 1,
        name: 'Iphone 12',
        price: 700,
        image: 'https://cdn.pixabay.com/photo/2016/11/20/08/33/camera-1842202__480.jpg'
    },
    {
        id: 2,
        name: 'Samsung s10',
        price: 400,
        image: 'https://cdn.pixabay.com/photo/2016/03/27/19/43/samsung-1283938__340.jpg'
    },
    {
        id: 3,
        name: 'Samsung Tv',
        price: 1200,
        image: 'https://cdn.pixabay.com/photo/2019/06/30/18/19/tv-4308538__480.jpg'
    },
    {
        id: 4,
        name: 'Huwawei Mate',
        price: 900,
        image: 'https://cdn.pixabay.com/photo/2017/08/11/14/19/honor-2631271__340.jpg'
    }
  ];
  const [productToShow, setProductToShow] = useState(2);


  const loadMoreProducts = () => {
      if(productToShow >= products.length){
          return;
      }else{
          setProductToShow(prevProductToShow => prevProductToShow + 2);
      }
  }

  return (
    <div className="row my-4">
        {
            products.slice(0, productToShow).map(product => (
              <div className="col-md-4" key={product.id}>
                <div className="card mb-2" style={{width: '18rem'}}>
                    <img src={product.image} className="card-img-top" alt={product.name} />
                    <div className="card-body">
                        <h5 className="card-title">{product.name}</h5>
                        <p className="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
                        <button className="btn btn-primary">
                            <i className="bi bi-cart-check-fill"></i> Add to cart
                        </button>
                    </div>
                </div>
            </div>
            ))
        }
        {
          productToShow < products.length && <div className="my-3">
              <button className="btn btn-danger" onClick={() => loadMoreProducts()}>
                  Looad more
              </button>
          </div>
        }
    </div>
  )
}

Related Tuorials

Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 5

In the last part of this tutorial, we will display the cart items, add the ability to increment/decr...


Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 4

In the fourth part of this tutorial, we will fetch and display all the products on the home page, an...


Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 3

In the third part of this tutorial, we will start coding the front end, first, we will install the p...


Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 2

In the second part of this tutorial, we will create the product and payment controllers, and later w...


Build a Shopping Cart Using React js Laravel 11 & Stripe Payment Gateway Part 1

In this tutorial, we will create a shopping cart using React js Laravel 11 and Stripe payment gatewa...


How to Use Rich Text Editor in React js

In this lesson, we will see how to use rich text editor in React JS, let's assume that we have a com...


How to Download a File from the Server Using Laravel and React js

In this tutorial, we will see how to download a file from the server using Laravel and React js, let...


How to Add a Class on Hover in React js

In this lesson, we will see how to add a class on hover in React js, let's assume that we have a boo...


Drag and Drop Image and File Upload Using React and Laravel

In this tutorial, we will see how to upload files using drag and drop in React js and Laravel, first...


API Authentication Using Laravel Sanctum and React js Part 3

In the third part of this tutorial, we will register and log in the user, get the access token, and...