变量不更改onclick
我在这里阅读了许多不同类型的解决方案,但似乎没有什么是我需要的。
当用户单击图像时,我正在尝试更改变量“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在,您正在尝试使用
CurrentIndex
使用Let currentIndex = 0;
。在React中,您需要使用状态
跟踪CurrentIndex
,以便您的其他组件(例如主图像)可以基于CurrentIndex
。而不是
让CurrentIndex = 0;
,currentIndex
带有usestate
之类的:确保导入
usestate
:从'react'';
import {usestate}然后,您可以更改
changeimage
函数以设置currentIndex的状态
这样:为什么这样做这边走?因为您需要触发 rerender
CurrentIndex
任何地方。除非A状态
更新在该组件中或父母中发生,否则是组件一部分的代码,这就是为什么console.logs 代码>和
图像
未更新。Right now you are trying to update
currentIndex
withlet currentIndex = 0;
. In React you will want to usestate
to keep track ofcurrentIndex
so that your other components (such as the main image) can changed based oncurrentIndex
.Instead of
let currentIndex = 0;
, initializecurrentIndex
withuseState
like this:Make sure to import
useState
as well:import {useState} from 'react';
Then you can change your
changeImage
function to set the state ofcurrentIndex
like this: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 astate
update occurs in that component or in a parent, which is why theconsole.logs
and theImage
weren't updating.