javascriptreactjsreact-flow

React-Flow: Can you pass props to a custom node in React-Flow?


I am using React-Flow in order to visualise a tree hierarchy of components in React. And whenever you create a custom node in React-Flow, you use it in the tree like that:

  <ReactFlow
      onLoad={onLoadTree}
      elements={nodesAndEdges}
      nodeTypes={{ reactComponent: ComponentNode }}

Which makes it impossible to pass props. Let's say that I want to pass something, then I would be able to do that if the syntax was like reactComponent: <ComponentNode myProps={myProps} />. Has anyone worked with this technology before and knows if it's somehow possible to pass props to a custom node? :) Thanks!


Solution

  • You can pass any data you want in the data property section when creating a new node :

    const newNode = {
      ...nodeProps,
      data: {
        myCustomProps: "test",
      },
    }