How to change image on button click in React js

Cynthia

React.js is a popular JavaScript library for building user interfaces, and it's commonly used for creating dynamic web applications. One common task is changing an image when a button is clicked. In this blog post, we'll walk through the steps to achieve this functionality in a React.js application.

Prerequisites

Before we get started, make sure you have the following:

  1. Node.js and npm: Ensure you have Node.js and npm (Node Package Manager) installed on your computer. You can download them from nodejs.org.

  2. React Project: You should have an existing React project or create a new one. You can create a new React app using Create React App or any other setup of your choice.

Step 1: Create a React App (If Needed)

If you don't have a React project, you can create one using Create React App. Open your terminal and run the following command:

how-to-change-image-on-button-click-in-react-js-1

Replace image-change-app with your preferred project name.

Step 2: Set Up Your Component

In your React project, create a new component that will contain the image and the button to change it. In the src directory, create a file named ImageChange.js:

how-to-change-image-on-button-click-in-react-js-2

In this component:

  • We initialize the currentImage state to 1. This state will keep track of the currently displayed image.

  • Inside the render() method, we render an <img> element with a source attribute that references the current image. The image file names are assumed to be image1.jpg, image2.jpg, and so on.

  • We also render a <button> element with an onClick event handler that will trigger the changeImage method when clicked. This method will be responsible for changing the image.

Step 3: Implement the changeImage Method

Inside the ImageChange component, implement the changeImage method to change the currentImage state. You can use conditional logic to cycle through the available images.

how-to-change-image-on-button-click-in-react-js-3

In this code:

  • We get the current image number from the currentImage state.

  • We define the total number of images you have in the totalImages variable (in this example, we assume there are 3 images).

  • Using modular arithmetic, we calculate the next image number by incrementing the current number and ensuring it loops back to 1 when it reaches the total number of images.

  • Finally, we update the currentImage state with the new image number, triggering a re-render and displaying the next image.

Step 4: Use the ImageChange Component

Now, integrate the ImageChange component into your main App.js file or wherever you want to use it:

how-to-change-image-on-button-click-in-react-js-4

Step 5: Run Your React App

Start your React application by running:

how-to-change-image-on-button-click-in-react-js-5

Open your app in a web browser, and you'll see the initial image displayed. Click the “Change Image” button, and the image will cycle to the next one in the sequence. Repeat the process to keep changing the image.

Congratulations! You've successfully implemented a feature to change an image on a button click in a React.js application. You can customize this further by adding more images and enhancing the user interface to suit your project's requirements.