仅使用ImageMapper组件(React-Img-Mapper)设置状态的故障设置状态
我正在尝试使用 reaction-img-mapper的ImageMapper组件创建交互式图像映射(基于 consclion> clickarea 进行更新 clickedareas
状态。
在该应用程序中,我呈现一个带有复选标记的列表,当相应的列表项目包含在 clickedareas
状态中时,复选标记变为绿色。
我致电 clickarea
通过 onclick 在列表项目本身上可以按预期工作,但是当我通过Imagemapper onclick prop ,我很难弄清原因。
注意: ImageMapper OnClick似乎正在使用最初设置的状态( usestate([])
),因为当我最初给它带有元素的数组时,它只是将其与该元素一起使用。 /em>
以下是有关来自React-Img-Mapper文档的OnClick Prop 的屏幕截图
这也是我的代码:
import React, { useState } from "react"
import ImageMapper from 'react-img-mapper';
function ImageMapQQ(props) {
const URL = "../images/qqmap.png"
const MAP = {
name: "qq-map",
areas: [
{ id: "NW1/4NW1/4", title: "NW1/4NW1/4", name: "NW1/4NW1/4", shape: "rect", coords: [0,0,50,50] },
{ id: "NE1/4NW1/4", title: "NE1/4NW1/4", name: "NE1/4NW1/4", shape: "rect", coords: [50,0,100,50] },
{ id: "NW1/4NE1/4", title: "NW1/4NE1/4", name: "NW1/4NE1/4", shape: "rect", coords: [100,0,150,50] },
{ id: "NE1/4NE1/4", title: "NE1/4NE1/4", name: "NE1/4NE1/4", shape: "rect", coords: [150,0,200,50] },
{ id: "SW1/4NW1/4", title: "SW1/4NW1/4", name: "SW1/4NW1/4", shape: "rect", coords: [0,50,50,100] },
{ id: "SE1/4NW1/4", title: "SE1/4NW1/4", name: "SE1/4NW1/4", shape: "rect", coords: [50,50,100,100] },
{ id: "SW1/4NE1/4", title: "SW1/4NE1/4", name: "SW1/4NE1/4", shape: "rect", coords: [100,50,150,100] },
{ id: "SE1/4NE1/4", title: "SE1/4NE1/4", name: "SE1/4NE1/4", shape: "rect", coords: [150,50,200,100] },
{ id: "NW1/4SW1/4", title: "NW1/4SW1/4", name: "NW1/4SW1/4", shape: "rect", coords: [0,100,50,150] },
{ id: "NE1/4SW1/4", title: "NE1/4SW1/4", name: "NE1/4SW1/4", shape: "rect", coords: [50,100,100,150] },
{ id: "NW1/4SE1/4", title: "NW1/4SE1/4", name: "NW1/4SE1/4", shape: "rect", coords: [100,100,150,150] },
{ id: "NE1/4SE1/4", title: "NE1/4SE1/4", name: "NE1/4SE1/4", shape: "rect", coords: [150,100,200,150] },
{ id: "SW1/4SW1/4", title: "SW1/4SW1/4", name: "SW1/4SW1/4", shape: "rect", coords: [0,150,50,200] },
{ id: "SE1/4SW1/4", title: "SE1/4SW1/4", name: "SE1/4SW1/4", shape: "rect", coords: [50,150,100,200] },
{ id: "SW1/4SE1/4", title: "SW1/4SE1/4", name: "SW1/4SE1/4", shape: "rect", coords: [100,150,150,200] },
{ id: "SE1/4SE1/4", title: "SE1/4SE1/4", name: "SE1/4SE1/4", shape: "rect", coords: [150,150,200,200] },
]
}
const areas = MAP.areas
const [clickedAreas, setClickedAreas] = useState([])
function clickArea(id) {
console.log("clickedAreas: ", clickedAreas)
if (clickedAreas.includes(id)) {
console.log("includes")
setClickedAreas(() => clickedAreas.filter(el => el != id))
}
else {
console.log("doesn't include")
setClickedAreas(() => [...clickedAreas, id])
}
}
return (
<>
<ImageMapper src={URL} map={MAP}
stayMultiHighlighted={true}
toggleHighlighted={true}
fillColor={"rgba(150, 213, 255, 0.6)"}
onClick={(area) => clickArea(area.id)} // THIS ONCLICK ISN'T WORKING CORRECTLY
/>
<div className="bold">AREAS</div>
{areas.map(area =>
<>
<div className="d-flex pointer" onClick={() => clickArea(area.id)}> {/* THIS ONCLICK IS WORKING CORRECTLY */}
<i className={`fas fa-check-circle ${clickedAreas.includes(area.id) ? "text-green" : "text-black opacity-10"}`}></i>
<div key={area.id}>{area.id}</div>
</div>
</>
)
}
</>
)
}
export default ImageMapQQ
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我仍然不知道为什么它没有原始写作,并且如果有人得到它,仍然会对它有更多的见解。
最终,ImageMapper OnClick只要单击一次点击时就一直使用初始状态(空数组) - 我可以说这是因为我有console.log当前
clickedareas
在ClickArea 单击图像映射时称为功能。
它似乎会更新状态(因为相应的检查标记变为绿色),但是每次调用ImageMapper OnClick函数时,它似乎总是从初始状态开始(Console.Log每次显示空数组)。
所有人都说,我找到了解决方法,可以根据需要进行工作。
而不是ImageMapper onClick调用
clickarea
apeariD ),触发使用效率,然后调用clickarea
函数,该功能后来设置了我实际想使用的状态,大声笑。 hacky,但起作用。(在每次点击时添加纳米型以使其独特,以便
setareAid
在Imagemapper OnClick中始终运行 - 如果我刚有aket.id。 。状态)小红色圆圈1-3是代码的新部分:
I still don't know why it doesn't work as originally written, and would still like more insight on that if anyone's got it.
Ultimately, the ImageMapper onClick just keeps using the initial state (empty array) whenever a click occurs -- I can tell this because I have it console.log the current
clickedAreas
state at the start of theclickArea
function that's called when the image map is clicked.It seems to update the state (because the corresponding checkmark turns green), but it seems to always start from the initial state every time the ImageMapper onClick function is called (console.log shows empty array every time).
That all being said, I HAVE found a workaround to make it work as desired.
Instead of the ImageMapper onClick calling the
clickArea
function that later sets state, I changed it to setting a different state immediately (areaId
), triggering a useEffect that then calls theclickArea
function that later sets the state I actually wanted to be working with, lol. Hacky, but works.(Added the nanoid in there to make it unique upon every click, so that the
setAreaId
function within ImageMapper onClick would always run -- if I just had area.id, it wouldn't run if that area.id was already theareaId
state.... aka, I couldn't get theclickArea
function to run to remove it from theclickedAreas
state)The small red circles 1-3 are the new parts of the code:
