•  


Custom Nodes ? React Flow
Learn
Customizing React Flow
Custom Nodes

Custom Nodes

A powerful feature of React Flow is the ability to add custom nodes. Within your custom nodes you can render everything you want. You can define multiple source and target handles and render form inputs or charts for example. In this section we will implement a node with an input field that updates some text in another part of the application.

Implementing the Custom Node

A custom node is a React component that is wrapped to provide basic functionality like selecting or dragging. From the wrapper component we are passing props like the position or the data besides other props . Let's start to implement the TextUpdaterNode . We are using the Handle component to be able to connect our custom node with other nodes and add an input field to the node:

import
 { useCallback } 
from
 'react'
;

import
 { Handle
,
 Position } 
from
 'reactflow'
;

 
const
 handleStyle
 =
 { left
:
 10
 };

 
function
 TextUpdaterNode
({ data }) {

  const
 onChange
 =
 useCallback
((evt) 
=>
 {

    console
.log
(
evt
.
target
.value);

  }
,
 []);

 
  return
 (

    <>

      <
Handle
 type
=
"target"
 position
=
{
Position
.Top} />

      <
div
>

        <
label
 htmlFor
=
"text"
>Text:</
label
>

        <
input
 id
=
"text"
 name
=
"text"
 onChange
=
{onChange} 
className
=
"nodrag"
 />

      </
div
>

      <
Handle
 type
=
"source"
 position
=
{
Position
.Bottom} 
id
=
"a"
 />

      <
Handle

        type
=
"source"

        position
=
{
Position
.Bottom}

        id
=
"b"

        style
=
{handleStyle}

      />

    </>

  );

}

As you see we've added the class name "nodrag" to the input. This prevents dragging within the input field and lets us select text for example.

Adding the Node Type

You can add a new node type to React Flow by adding it to the nodeTypes prop. It's important that the nodeTypes are memoized or defined outside of the component. Otherwise React creates a new object on every render which leads to performance issues and bugs.

const
 nodeTypes
 =
 useMemo
(() 
=>
 ({ textUpdater
:
 TextUpdaterNode })
,
 []);

 
return
 <
ReactFlow
 nodeTypes
=
{nodeTypes} />;

After defining your new node type, you can use it by using the type node option:

const
 nodes
 =
 [

  {

    id
:
 'node-1'
,

    type
:
 'textUpdater'
,

    position
:
 { x
:
 0
,
 y
:
 0
 }
,

    data
:
 { value
:
 123
 }
,

  }
,

];

After putting all together and adding some basic styles we get a custom node that prints text to the console:

Using Multiple Handles

As you can see we added two source handles to the node so that it has two outputs. If you want to connect other nodes with these specific handles, the node id is not enough but you also need to pass the specific handle id. In this case one handle has the id "a" and the other one "b" . Handle specific edges use the sourceHandle or targetHandle options that reference a handle within a node:

const
 initialEdges
 =
 [

  { id
:
 'edge-1'
,
 source
:
 'node-1'
,
 sourceHandle
:
 'a'
,
 target
:
 'node-2'
 }
,

  { id
:
 'edge-2'
,
 source
:
 'node-1'
,
 sourceHandle
:
 'b'
,
 target
:
 'node-3'
 }
,

];

In this case the source node is node-1 for both handles but the handle ids are different. One comes from handle id "a" and the other one from "b" . Both edges also have different target nodes:

Note that if you are programmatically changing the position or number of handles in your custom node, you will need to use the useUpdateNodeInternals hook to properly notify ReactFlow of changes. From here you should be able to build your custom nodes. In most cases we recommend to use custom nodes only. The built-in ones are just basic examples. You can find a list of the passed props and more information in the custom node API section .

- "漢字路" 한글한자자동변환 서비스는 교육부 고전문헌국역지원사업의 지원으로 구축되었습니다.
- "漢字路" 한글한자자동변환 서비스는 전통문화연구회 "울산대학교한국어처리연구실 옥철영(IT융합전공)교수팀"에서 개발한 한글한자자동변환기를 바탕하여 지속적으로 공동 연구 개발하고 있는 서비스입니다.
- 현재 고유명사(인명, 지명등)을 비롯한 여러 변환오류가 있으며 이를 해결하고자 많은 연구 개발을 진행하고자 하고 있습니다. 이를 인지하시고 다른 곳에서 인용시 한자 변환 결과를 한번 더 검토하시고 사용해 주시기 바랍니다.
- 변환오류 및 건의,문의사항은 juntong@juntong.or.kr로 메일로 보내주시면 감사하겠습니다. .
Copyright ⓒ 2020 By '전통문화연구회(傳統文化硏究會)' All Rights reserved.
 한국   대만   중국   일본