How to add Previous and Next Buttons in your React Project

To add previous and next buttons in a React project, you can follow these steps:

  1. Create a state variable to keep track of the current index.
const [currentIndex, setCurrentIndex] = useState(0);
  1. Define two functions to handle the previous and next button clicks. These functions should update the currentIndex state variable accordingly.
const handlePrevClick = () => {
  if (currentIndex > 0) {
    setCurrentIndex(currentIndex - 1);
  }
};

const handleNextClick = () => {
  if (currentIndex < data.length - 1) {
    setCurrentIndex(currentIndex + 1);
  }
};

Note that data represents the array of items you want to display.

  1. Render the previous and next buttons in your component, and attach the event handlers you just defined.
return (
  <div>
    <button onClick={handlePrevClick}>Previous</button>
    <button onClick={handleNextClick}>Next</button>
    <p>{data[currentIndex]}</p>
  </div>
);

This example assumes that you have a data array that contains the items you want to display. The current item is displayed using data[currentIndex]. When the user clicks the previous or next button, the currentIndex state variable is updated accordingly.

Of course, you can modify this code to fit your specific needs. For example, you might want to disable the previous button when the user is on the first item, or disable the next button when the user is on the last item.

Lets take another example-

  1. Create a state variable to keep track of the current photo index and an array of photo URLs.
const [currentPhotoIndex, setCurrentPhotoIndex] = useState(0);

const photos = [
  'https://example.com/photo1.jpg',
  'https://example.com/photo2.jpg',
  'https://example.com/photo3.jpg',
  // ...
];
  1. Define two functions to handle the previous and next button clicks. These functions should update the current photo index state variable accordingly.
const handlePrevClick = () => {
  if (currentPhotoIndex > 0) {
    setCurrentPhotoIndex(currentPhotoIndex - 1);
  }
};

const handleNextClick = () => {
  if (currentPhotoIndex < photos.length - 1) {
    setCurrentPhotoIndex(currentPhotoIndex + 1);
  }
};
  1. Render the previous and next buttons in your component, and attach the event handlers you just defined.
return (
  <div>
    <button onClick={handlePrevClick}>Previous</button>
    <button onClick={handleNextClick}>Next</button>
    <img src={photos[currentPhotoIndex]} alt="Current photo" />
  </div>
);

This example assumes that you have an array of photo URLs called photos. The current photo is displayed using <img src={photos[currentPhotoIndex]} alt="Current photo" />. When the user clicks the previous or next button, the currentPhotoIndex state variable is updated accordingly.

Note that in this example, we're checking whether currentPhotoIndex is greater than 0 before decrementing it in handlePrevClick, and checking whether it's less than photos.length - 1 before incrementing it in handleNextClick. This prevents the user from going out of bounds when navigating the gallery.