javascript - 使用ant design在react js中映射元素
问题描述
大家好,我在 ant-design 中映射模态时遇到问题
import React, { useState } from "react";
import { Modal } from "antd";
import "./style.css";
import "antd/dist/antd.css";
export default function App() {
const arr = [
{
name: "siddharth",
id: 32
},
{
name: "sanjay",
id: 34
}
];
const [modalstate, setModalstate] = useState({
edit: false,
remove: false
});
return (
<div>
<h1>Hello StackBlitz!</h1>
<p>Start editing to see some magic happen :)</p>
{arr.map(x => (
<>
<div>
{x.name}
<button
onClick={() => setModalstate({ ...modalstate, edit: true })}
>
Edit
</button>
<button
onClick={() => setModalstate({ ...modalstate, remove: true })}
>
Remove
</button>
</div>
<Modal
visible={modalstate.edit}
onCancel={() => setModalstate({ ...modalstate, edit: false })}
>
{x.name}
{"EDIT "}
</Modal>
<Modal
visible={modalstate.remove}
onCancel={() => setModalstate({ ...modalstate, remove: false })}
>
{x.name}
{"REMOVE"}
</Modal>
</>
))}
</div>
);
}
在此代码中,问题是当我单击第一项按钮时,编辑/删除它会为第二项打开。因此,每个组件的模态都没有被渲染,只有最后一个被渲染 https://react-vzh4yd.stackblitz.io
解决方案
推荐阅读
- reactjs - 将 Marketo 表单添加到 Gatsby/JSX
- javascript - 使用 javascript id 更改 CSS 类会破坏 CSS 样式?
- grails - Grails:找不到创建控制器?
- android - 是否可以为少数人开发应用程序?
- java - java中的setSoTimeout方法改变了哪个定时器?
- java - onStart() 中的无限循环
- jsf - f:param 渲染时返回 null
- jquery - 如何使用 jquery 在 div 中编写原始 html?
- java - Spring MVC中如何通过Controller将数据从MongoDB转换到View
- php - 更新帖子在与更新用户一起使用时不起作用