点击时如何更改数组中的图像。

huangapple go评论56阅读模式
英文:

how to change the image in the array when we click

问题

我有一点小问题

我希望每次点击图像时,我的主图像会根据我点击的图像而更改

我希望点击下面的图像,上面的图像会更改

这是我的代码

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>
  );
}

希望这对你有所帮助。

英文:

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

点击时如何更改数组中的图像。

this is my code

import React from &quot;react&quot;;

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

答案1

得分: 3

你需要将state设置为所选图像的index,然后根据clicked项更改值。

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>
  );
}
英文:

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 &quot;react&quot;;

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

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

huangapple
  • 本文由 发表于 2023年2月23日 20:23:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定