为什么边缘在反应流中不渲染?

问题描述 投票:0回答:2

我是反应流新手。我的目标是连接到相同类型的节点。我想要一个节点模板并在任何地方使用它。 下面是简单的示例,但没有出现边缘。

如果我删除节点中的类型,一切都会很好。

import { useCallback, useState } from "react";
import "reactflow/dist/style.css";
import TextUpdaterNode from "./TextUpdaterNode.js";
import "./text-updater-node.css";

const rfStyle = {
  backgroundColor: "#B8CEFF"
};

const initialNodes = [
  {
    id: "1",
    position: { x: 0, y: 0 },
    type: "textUpdater"
  },
  {
    id: "2",
    position: { x: 0, y: 100 },
    type: "textUpdater"
  }
];

const initialEdges = [{ id: "e1-2", source: "1", target: "2", animated: true }];

const nodeTypes = { textUpdater: TextUpdaterNode };

function Flow() {
  return (
    <ReactFlow
      nodes={initialNodes}
      edges={initialEdges}
      nodeTypes={nodeTypes}
      fitView
      style={rfStyle}
    />
  );
}

export default Flow;
reactjs react-flow
2个回答
0
投票

边缘未出现的问题可能是由于 TextUpdaterNode 组件的定义或注册方式导致的


0
投票

要解决您的问题,您需要确保正确实现

TextUpdaterNode component
,并在ReactFlow的nodeTypes属性中注册自定义节点类型。

TextUpdaterNode:

import React from 'react';
import { Handle, Position } from 'reactflow';

const TextUpdaterNode = ({ data }) => {
  return (
    <div className="text-updater-node">
      <Handle type="target" position={Position.Top} />
      <div>{data.label}</div>
      <Handle type="source" position={Position.Bottom} />
    </div>
  );
};

export default TextUpdaterNode;

对于 flow.js:

import React from 'react';
import ReactFlow from 'reactflow';
import 'reactflow/dist/style.css';
import TextUpdaterNode from './TextUpdaterNode';
import './text-updater-node.css';

const rfStyle = {
  backgroundColor: '#B8CEFF',
};

const initialNodes = [
  {
    id: '1',
    position: { x: 0, y: 0 },
    type: 'textUpdater',
    data: { label: 'Node 1' },
  },
  {
    id: '2',
    position: { x: 0, y: 100 },
    type: 'textUpdater',
    data: { label: 'Node 2' },
  },
];

const initialEdges = [
  { id: 'e1-2', source: '1', target: '2', animated: true },
];

const nodeTypes = { textUpdater: TextUpdaterNode };

function Flow() {
  return (
    <ReactFlow
      nodes={initialNodes}
      edges={initialEdges}
      nodeTypes={nodeTypes}
      fitView
      style={rfStyle}
    />
  );
}

export default Flow;

您还可以添加以下 CSS:

.text-updater-node {
  padding: 10px;
  border: 1px solid #ddd;
  background: #fff;
  border-radius: 3px;
}
© www.soinside.com 2019 - 2024. All rights reserved.