Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to change the image in the array when we click

I’m having a little problem

I want every time I click on an image, my main image will change according to the image that I click on

I want to click on the image below, the image above changes

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

this is my code

import React from "react";

const SingleProductComponent = ({ list }) => {
  return (
 
      
                <div className="col-12 col-sm-12 col-md-6 col-lg-6 col-xl-6">
                  <div className="SingProduct__img">
                    <div className="SingProduct__inner">
                      <div className="inner__data">
                        {list.image && (
                          <img
                            src={list.image[0]}
                            alt=""
                            className="inner__data-img"
                          />
                        )}
                      </div>
                    </div>
                    {list.image && (
                      <div className="SingleProduct__thumb">
                        <div className="SingleProduct__thumb-list">
                          {list.image.map((e, index) => (
                            <div className="thumb__list-item" key={index}>
                              <img src={e} className="thumb__item-img" alt="" />
                            </div>
                          ))}
                        </div>
                      </div>
                    )}
                  </div>
                </div>
  )
}

>Solution :

You need to set a state to the selected image index, and then according to the clicked item, you change the value.

import React, {useState} from "react";

const SingleProductComponent = ({ list }) => {
  const [selectedIndex, setSelectedIndex] = useState(0);

  const changeSelectedItem = (index) => {
    setSelectedIndex(index)
  }
  return (
                <div className="col-12 col-sm-12 col-md-6 col-lg-6 col-xl-6">
                  <div className="SingProduct__img">
                    <div className="SingProduct__inner">
                      <div className="inner__data">
                        {list.image && (
                          <img
                            src={list.image[selectedIndex]}
                            alt=""
                            className="inner__data-img"
                          />
                        )}
                      </div>
                    </div>
                    {list.image && (
                      <div className="SingleProduct__thumb">
                        <div className="SingleProduct__thumb-list">
                          {list.image.map((e, index) => (
                            <div className="thumb__list-item" key={index} onClick={() => changeSelectedItem(index)}>
                              <img src={e} className="thumb__item-img" alt="" />
                            </div>
                          ))}
                        </div>
                      </div>
                    )}
                  </div>
                </div>
  )
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading