【发布时间】:2021-07-08 20:26:18
【问题描述】:
如何保持 div 的第一个元素默认被点击?我在下面给出了一个包含图像的 div,我希望默认单击 div 的第一个图像。我将其用作反应功能组件中的视图
```<div className="tv_tabs" style={{ marginLeft: "110px" }}>
{
covidhealthImage && covidhealthImage.map((image, index) => {
return (
<div><img height="70" width="70" onClick={() => showHeading(index)} src={image} /></div>
);
})
}
</div>```
const showHeading = (index) => {
const heading = healthHeadline[index]
const headingImage = covidhealthImage[index]
const captionText = healthCaption[index]
setNewImage(headingImage)
setNewCaption(captionText)
setCurrentHeading(heading)
}
const [newImage, setNewImage] = useState([]);
const [newCaption, setNewCaption] = useState([]);
const [healthHeadline, setHealthHeadline] = useState([])
const [currentHeading, setCurrentHeading] = useState('')
【问题讨论】:
-
你看错了。您希望您的状态反映点击所做的任何事情。我们不知道 showHeading() 在您的州实际上做了什么
-
@charlietfl 你现在可以检查一下吗,我已经添加了更多详细信息。
-
所以基本上你想要什么。如果我理解正确,就是将各种 useStates 的默认值设置为调用
showHeading(0)时设置的值
标签: javascript html reactjs react-hooks