javascript - React Flow:如何删除元素?
问题描述
我在通过按钮删除反应流中的元素时遇到问题。
我可以使用 Backspace 很好地删除元素,但该按钮仅适用于第一次删除,之后它会恢复已删除的节点。
使用 react-flow 的新手,在这里无法解决问题。状态没有改变吗?
下面是我用于反应流的代码
import React, { useState, useCallback, useEffect } from "react";
import ReactFlow, {
removeElements,
addEdge,
Background,
} from "react-flow-renderer";
const onLoad = (reactFlowInstance) => {
reactFlowInstance.fitView();
console.log(reactFlowInstance.getElements());
};
const StackFlow = () => {
const initialElements = [
{
id: "0",
position: { x: 0, y: -100 },
sourcePosition: "bottom",
style: {
width: 100,
fontSize: 11,
color: "white",
background: "#6ec9c0",
},
data: {
label: (
<>
<button
className="w-md h-md border-2 border-black p-2"
onClick={() =>
remModelData("0")
}
>
Del
</button> <br /> <br />
<strong>Models</strong>
</>
),
},
},
{
id: "1",
position: { x: 100, y: 50 },
sourcePosition: "bottom",
targetPosition: "top",
data: {
label: (
<>
<button
className="w-md h-md border-2 border-black p-2"
onClick={() =>
remModelData("1")
}
>
Del
</button> <br /> <br />
Model: <strong>1</strong> <br />
ID: 1
</>
),
},
},
{
id: "2",
position: { x: 150, y: 250 },
sourcePosition: "bottom",
targetPosition: "top",
data: {
label: (
<>
<button
className="w-md h-md border-2 border-black p-2"
onClick={() =>
remModelData("2")
}
>
Del
</button> <br /> <br />
Model 1: <strong>subModel1</strong> <br />
ID: 2
</>
),
},
},
{
id: "3",
position: { x: 250, y: 250 },
sourcePosition: "bottom",
targetPosition: "top",
data: {
label: (
<>
<button
className="w-md h-md border-2 border-black p-2"
onClick={() =>
remModelData("3")
}
>
Del
</button> <br /> <br />
Model 1: <strong>subModel2</strong> <br />
ID: 3
</>
),
},
},
{
id: "0-1",
type: "step",
source: "0",
target: "1"
},
{
id: "1-2",
type: "step",
source: "1",
target: "2"
},
{
id: "1-3",
type: "step",
source: "1",
target: "3"
},
];
const [elements, setElements] = useState(initialElements);
const onElementsRemove = useCallback(
(elementsToRemove) =>
setElements((els) => removeElements(elementsToRemove, els)),
[]
);
const onConnect = (params) => setElements((els) => addEdge(params, els));
const [reactflowInstance, setReactflowInstance] = useState(null);
useEffect(() => {
if (reactflowInstance && elements.length > 0) {
reactflowInstance.fitView();
}
}, [reactflowInstance, elements.length]);
const remModelData = useCallback((id) => {
let arr = elements
var index = arr.indexOf(id);
if (index > -1) {
arr.splice(index, 1);
}
arr = arr.filter(function (obj) {
return obj.id !== id;
});
console.log(arr);
setElements(arr);
}, []);
return (
<ReactFlow
elements={elements}
onElementsRemove={onElementsRemove}
onConnect={onConnect}
onLoad={onLoad}
snapToGrid={true}
snapGrid={[15, 15]}
>
<Background color="#aaa" gap={16} />
</ReactFlow>
);
};
function Home() {
return (
<>
<div className="w-full mx-auto justify-center items-center flex">
<div
className="flex mt-10 flex-row items-center content-center justify-center max-h-5xl max-w-5xl py-2"
style={{ flex: 1, width: 1000, height: 800, borderWidth: 2 }}
>
<StackFlow />
</div>
</div>
</>
);
}
export default Home;
解决方案
通过创建这个函数,我能够完成我想要的最终结果:
const deleteNode = (id) => {
setElements((els) => removeElements([elements[id]], els));
};
并将其传递给 OnClick:
onClick={() => deleteNode(0)}
将 0 更改为您希望从数组中删除的元素的索引
推荐阅读
- batch-file - Batch For Loop 不在指定分隔符处分隔字符串
- android - 实现日历和时间选择器的最佳方法是什么?
- php - 谷歌日历 api (php) 和 GitHub 文件丢失
- angular - 没有将“exportAs”设置为“ngForm”的指令 - 不使用解决方案
- java - 无法在函数“public ArrayList”中对数组类型 char[] 调用 copyOf(char[], int)
排序(数组列表 字符)" - java - 流口水可以使用哪些不同的方言?
- c++ - 使用 SSE 的 interleave shuffle 原理
- bash - 与本地相比,Grep 在 Travis 上的行为不同
- c# - 在 Unity2d 中,我有一个由 rigibody addforce 移动的圆圈。如果它是移动平台的孩子,它会变形并且比例会变得奇怪
- excel - 如何将我已经拥有的工作簿保存为 CSV,并使用用户通过输入框提供的新名称?