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

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

how to change the image in the array when we click

问题

我有一点小问题

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

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

这是我的代码

  1. import React from "react";
  2. const SingleProductComponent = ({ list }) => {
  3. return (
  4. <div className="col-12 col-sm-12 col-md-6 col-lg-6 col-xl-6">
  5. <div className="SingProduct__img">
  6. <div className="SingProduct__inner">
  7. <div className="inner__data">
  8. {list.image && (
  9. <img
  10. src={list.image[0]}
  11. alt=""
  12. className="inner__data-img"
  13. />
  14. )}
  15. </div>
  16. </div>
  17. {list.image && (
  18. <div className="SingleProduct__thumb">
  19. <div className="SingleProduct__thumb-list">
  20. {list.image.map((e, index) => (
  21. <div className="thumb__list-item" key={index}>
  22. <img src={e} className="thumb__item-img" alt="" />
  23. </div>
  24. ))}
  25. </div>
  26. </div>
  27. )}
  28. </div>
  29. </div>
  30. );
  31. }

希望这对你有所帮助。

英文:

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

  1. import React from &quot;react&quot;;
  2. const SingleProductComponent = ({ list }) =&gt; {
  3. return (
  4. &lt;div className=&quot;col-12 col-sm-12 col-md-6 col-lg-6 col-xl-6&quot;&gt;
  5. &lt;div className=&quot;SingProduct__img&quot;&gt;
  6. &lt;div className=&quot;SingProduct__inner&quot;&gt;
  7. &lt;div className=&quot;inner__data&quot;&gt;
  8. {list.image &amp;&amp; (
  9. &lt;img
  10. src={list.image[0]}
  11. alt=&quot;&quot;
  12. className=&quot;inner__data-img&quot;
  13. /&gt;
  14. )}
  15. &lt;/div&gt;
  16. &lt;/div&gt;
  17. {list.image &amp;&amp; (
  18. &lt;div className=&quot;SingleProduct__thumb&quot;&gt;
  19. &lt;div className=&quot;SingleProduct__thumb-list&quot;&gt;
  20. {list.image.map((e, index) =&gt; (
  21. &lt;div className=&quot;thumb__list-item&quot; key={index}&gt;
  22. &lt;img src={e} className=&quot;thumb__item-img&quot; alt=&quot;&quot; /&gt;
  23. &lt;/div&gt;
  24. ))}
  25. &lt;/div&gt;
  26. &lt;/div&gt;
  27. )}
  28. &lt;/div&gt;
  29. &lt;/div&gt;
  30. )
  31. }

答案1

得分: 3

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

  1. import React, { useState } from "react";
  2. const SingleProductComponent = ({ list }) => {
  3. const [selectedIndex, setSelectedIndex] = useState(0);
  4. const changeSelectedItem = (index) => {
  5. setSelectedIndex(index)
  6. }
  7. return (
  8. <div className="col-12 col-sm-12 col-md-6 col-lg-6 col-xl-6">
  9. <div className="SingProduct__img">
  10. <div className="SingProduct__inner">
  11. <div className="inner__data">
  12. {list.image && (
  13. <img
  14. src={list.image[selectedIndex]}
  15. alt=""
  16. className="inner__data-img"
  17. />
  18. )}
  19. </div>
  20. </div>
  21. {list.image && (
  22. <div className="SingleProduct__thumb">
  23. <div className="SingleProduct__thumb-list">
  24. {list.image.map((e, index) => (
  25. <div className="thumb__list-item" key={index} onClick={() => changeSelectedItem(index)}>
  26. <img src={e} className="thumb__item-img" alt="" />
  27. </div>
  28. ))}
  29. </div>
  30. </div>
  31. )}
  32. </div>
  33. </div>
  34. );
  35. }
英文:

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

  1. import React, {useState} from &quot;react&quot;;
  2. const SingleProductComponent = ({ list }) =&gt; {
  3. const [selectedIndex, setSelectedIndex] = useState(0);
  4. const changeSelectedItem = (index) =&gt; {
  5. setSelectedIndex(index)
  6. }
  7. return (
  8. &lt;div className=&quot;col-12 col-sm-12 col-md-6 col-lg-6 col-xl-6&quot;&gt;
  9. &lt;div className=&quot;SingProduct__img&quot;&gt;
  10. &lt;div className=&quot;SingProduct__inner&quot;&gt;
  11. &lt;div className=&quot;inner__data&quot;&gt;
  12. {list.image &amp;&amp; (
  13. &lt;img
  14. src={list.image[selectedIndex]}
  15. alt=&quot;&quot;
  16. className=&quot;inner__data-img&quot;
  17. /&gt;
  18. )}
  19. &lt;/div&gt;
  20. &lt;/div&gt;
  21. {list.image &amp;&amp; (
  22. &lt;div className=&quot;SingleProduct__thumb&quot;&gt;
  23. &lt;div className=&quot;SingleProduct__thumb-list&quot;&gt;
  24. {list.image.map((e, index) =&gt; (
  25. &lt;div className=&quot;thumb__list-item&quot; key={index} onClick={() =&gt; changeSelectedItem(index)}&gt;
  26. &lt;img src={e} className=&quot;thumb__item-img&quot; alt=&quot;&quot; /&gt;
  27. &lt;/div&gt;
  28. ))}
  29. &lt;/div&gt;
  30. &lt;/div&gt;
  31. )}
  32. &lt;/div&gt;
  33. &lt;/div&gt;
  34. )
  35. }

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:

确定