我有一个JSON数组,其中包含一些带有图像数组的对象和一些带有单个图像的对象.例如:

[  
   {
     "title": "Title",
     "img_src": "example.png"  
     },
   {
     "title": "Title2",
     "img_count": 2,
     "img_src": [
       "example2.png",
       "example3.png"
     ]  
   },
   {
     "title": "Title3",
     "img_src": "example4.png"  
     },
   {
     "title": "Title4",
     "img_count": 3,
     "img_src": [
       "example5.png",
       "example6.png",
       "example7.png"
     ]  
   }
]

现在,我在两个按钮之间显示图像,如何通过这些按钮更改特定对象(从JSON检索)的图像?

这就是我所做的:

export default function Products({ jsonArr }) {
  const [counter, setCounter] = useState(0);

  function toggleImage(index) {
      setCounter(counter === 0 ? 1 : 0); //This is just for testing.
  }

.........
........

<div className="parentElement">
              {jsonArr.map((arrEl, i, _) => {
                return (
                  <div key={`${i}`} className="row" style={{ marginBottom: "50px" }}>
                    <div className="col-sm-4">
                      {arrEl.img_count && (
                        <button onClick={() => toggleImage(i)}>
                          <iconify-icon icon="ep:arrow-left-bold"></iconify-icon>
                        </button>
                      )}

                      {arrEl.img_count ? (
                        <img key={arrEl.title} src={arrEl.img_src[counter]} className="cover" height="250" width="250" alt="products"></img>
                      ) : (
                        <img key={arrEl.title} src={arrEl.img_src} className="cover" height="250" width="250" alt="products"></img>
                      )}
                      {arrEl.img_count && (
                        <button onClick={() => toggleImage(i)}>
                          <iconify-icon icon="ep:arrow-right-bold"></iconify-icon>
                        </button>
                      )}
                    </div>
...more code here...

我只在对象中存在'img_count'时才显示按钮."toggleImage"按钮功能正在工作,但它正在更改数组中所有对象的图像.

请建议一些更好的方法来做到这一点.或者这一点是否可以纠正.

推荐答案

目前,代码只有一个counter状态,102映射jsonArr数组对象正在引用,所以它们都同时更改/更新.

如果希望每个映射的元素都有自己的状态,那么将JSX抽象为一个Reaction组件,并将counter状态移动到每个元素中.

例如:

const MyComponent = ({ data }) => {
  const [index, setIndex] = useState(0);

  const nextImage = () => {
    setIndex(index => index + 1);
  };

  const prevImage = () => {
    setIndex(index => index - 1);
  };

  return (
    <div className="row" style={{ marginBottom: "50px" }}>
      <div className="col-sm-4">
        {data.img_count && (
          <button onClick={prevImage}>
            <iconify-icon icon="ep:arrow-left-bold" />
          </button>
        )}

        <img
          key={data.title}
          src={data.img_count
            ? data.img_src[index % data.img_src.length]
            : data.img_src
          }
          className="cover"
          height="250"
          width="250"
          alt="products"
        />

        {data.img_count && (
          <button onClick={nextImage}>
            <iconify-icon icon="ep:arrow-right-bold" />
          </button>
        )}
      </div>
    </div>
  );
}
<div className="parentElement">
  {jsonArr.map((arrEl, i) => (
    <MyComponent key={i} data={arrEl} />
  )}
</div>

Javascript相关问答推荐

fetch在本地设置相同来源的cookie,但部署时相同的代码不会设置cookie

使用print This时, map 容器已在LeafletJS中初始化

有条件的悲剧

使用续集和下拉栏显示模型属性

传递一个大对象以在Express布局中呈现

GrapeJS -如何保存和加载自定义页面

判断表格单元格中是否存在文本框

构造HTML表单以使用表单数据创建对象数组

显示图—如何在图例项上添加删除线效果?

如何发送从REST Api收到的PNG数据响应

当输入字段无效时,我的应用程序不会返回错误

映射类型定义,其中值对应于键

Phaserjs-创建带有层纹理的精灵层以自定义外观

我正在试着做一个TicTacToe Ai来和我玩.但是,我试着在第一个方块被点击时出现一个X,然后在第二个方块之后出现一个O

无法使用npm install安装react-dom、react和next

使用Perl Selify::Remote::Driver执行Java脚本时出错

如何使用fltter_js将对象作为参数传递给javascrip?

在对象的嵌套数组中添加两个属性

每隔一行文本段落进行镜像(Boustrophedon)

与find()方法一起使用时,Mongoose中的$or运算符没有提供所有必需的数据