变量不更改onclick

发布于 2025-01-22 12:20:17 字数 1779 浏览 4 评论 0原文

我在这里阅读了许多不同类型的解决方案,但似乎没有什么是我需要的。

当用户单击图像时,我正在尝试更改变量“ CurrentIndex”。

目前,更改已在OnClick函数内部到达,但不会更改外部变量。不知道我在做什么错。

我的最终目标是让用户能够单击缩略图图像并更改主图像。如果有更好的方法,请愿意。

const gallery = 
  product.images.length > 1 ? (
    <Grid gap={2} columns={4}>
      {product.images.map((img, index) => (
        <Thumbnail 
          image={img}
          key={img.id}
          alt={product.title}
          index={index}
          onClick={() => changeImage(index)}
        />
      ))}
    </Grid>
  ) : null;

let currentIndex = 0;

let changeImage = function(index) {
  currentIndex = index;

  console.log("currentindex inside changeImage Function is: " + currentIndex) // This returns the current index of the image clicked as it should.

  return currentIndex;
}

console.log("currentindex outside changeImage Function is: " + currentIndex)// This returns "0" still

下面的代码是主要图像。

<div>
  {console.log('currentindex inside return is: ' + currentIndex)}//This returns 0 as well still

  <GatsbyImage
    image={product.images[currentIndex].gatsbyImageData}
    alt={product.title}
  />          
  <div>
    {gallery}
  </div>
</div>

不确定是否需要此代码,但以下是“缩略图”元素的代码

/** @jsx jsx */
import { jsx } from 'theme-ui'
import { GatsbyImage } from 'gatsby-plugin-image'

export const Thumbnail = ({ image, onClick, alt, index }) => {

  return (
    <button
      sx={{
        cursor: "pointer",
        border: "none",
        padding: "2px",
        backgroundColor: "white",
      }}
      onClick={onClick}
    >
      <GatsbyImage
        image={image.gatsbyImageData}
        alt={alt}
      />
    </button>
  )
}

I have read through a lot of different types of solutions here but none seem to be quite what I need.

I am trying to change the variable "currentIndex" when a user clicks the image.

Right now the change reaches inside the onClick function but does not change the outside variable. Not sure what I am doing wrong.

My end goal is to have user be able to click an thumbnail image and have it change the main image. If there is a better way PLEASE I am open to that.

const gallery = 
  product.images.length > 1 ? (
    <Grid gap={2} columns={4}>
      {product.images.map((img, index) => (
        <Thumbnail 
          image={img}
          key={img.id}
          alt={product.title}
          index={index}
          onClick={() => changeImage(index)}
        />
      ))}
    </Grid>
  ) : null;

let currentIndex = 0;

let changeImage = function(index) {
  currentIndex = index;

  console.log("currentindex inside changeImage Function is: " + currentIndex) // This returns the current index of the image clicked as it should.

  return currentIndex;
}

console.log("currentindex outside changeImage Function is: " + currentIndex)// This returns "0" still

Code below is for main image.

<div>
  {console.log('currentindex inside return is: ' + currentIndex)}//This returns 0 as well still

  <GatsbyImage
    image={product.images[currentIndex].gatsbyImageData}
    alt={product.title}
  />          
  <div>
    {gallery}
  </div>
</div>

Not sure if this code is needed but below is the code for the "Thumbnail" element

/** @jsx jsx */
import { jsx } from 'theme-ui'
import { GatsbyImage } from 'gatsby-plugin-image'

export const Thumbnail = ({ image, onClick, alt, index }) => {

  return (
    <button
      sx={{
        cursor: "pointer",
        border: "none",
        padding: "2px",
        backgroundColor: "white",
      }}
      onClick={onClick}
    >
      <GatsbyImage
        image={image.gatsbyImageData}
        alt={alt}
      />
    </button>
  )
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断念 2025-01-29 12:20:17

现在,您正在尝试使用CurrentIndex使用Let currentIndex = 0;。在React中,您需要使用状态跟踪CurrentIndex ,以便您的其他组件(例如主图像)可以基于CurrentIndex

而不是让CurrentIndex = 0;currentIndex带有usestate之类的:

const [currentIndex, setCurrentIndex] = useState(0);

确保导入usestate从'react''; import {usestate}

然后,您可以更改changeimage函数以设置currentIndex的状态这样:

let changeImage = function(index) {
  setCurrentIndex(index);
}

为什么这样做这边走?因为您需要触发 rerender CurrentIndex任何地方。除非A 状态更新在该组件中或父母中发生,否则是组件一部分的代码,这就是为什么console.logs 代码>和图像未更新。

Right now you are trying to update currentIndex with let currentIndex = 0;. In React you will want to use state to keep track of currentIndex so that your other components (such as the main image) can changed based on currentIndex.

Instead of let currentIndex = 0;, initialize currentIndex with useState like this:

const [currentIndex, setCurrentIndex] = useState(0);

Make sure to import useState as well: import {useState} from 'react';

Then you can change your changeImage function to set the state of currentIndex like this:

let changeImage = function(index) {
  setCurrentIndex(index);
}

Why do it this way? Because you need to trigger a rerender to use the newly updated currentIndex anywhere. Code that is part of a component is only run once unless a state update occurs in that component or in a parent, which is why the console.logs and the Image weren't updating.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文