2

I'm using 'reactflow' library in my react project I'm trying to find a way to use an image in place of node so i can drag and move the image i want and also connect two images via edges I have gone through the docs of reactflow but didn't find a solution I don't know if its supported by library or not Looking forward for any help.

1
  • 2
    You should share a minimal code to work with. But in your case, You've to create a custom node, as explained in the doc. Commented Jul 23, 2023 at 2:48

1 Answer 1

4

It is supported by the React Flow because React Flow's "custom nodes are just React components, you can implement anything you need".

You can create a custom node with a div as the node and then set the background-image of the div, via CSS, with whatever you'd like. (Alternatively, you can use an img element and set the src property.)

background-image example:

import React, { memo } from "react";
import { Handle, Position } from "reactflow";

export default memo(({ data, isConnectable }) => {
  return (
    <>
      <Handle
        type="target"
        position={Position.Left}
        isConnectable={isConnectable}
      />
      <div
        style={{
          height: data.image.height,
          width: data.image.width,
          backgroundImage: `url(${data.image.url})`,
          backgroundRepeat: "no-repeat"
        }}
      />
      <Handle
        type="source"
        position={Position.Right}
        id="a"
        isConnectable={isConnectable}
      />
    </>
  );
});

Working CodeSandbox: https://codesandbox.io/s/react-flow-image-node-92kxwn?file=/ImageNode.js:0-789

Or

<img /> example:

import React, { memo } from "react";
import { Handle, Position } from "reactflow";

export default memo(({ data, isConnectable }) => {
  return (
    <>
      <Handle
        type="target"
        position={Position.Left}
        onConnect={(params) => console.log("handle onConnect", params)}
        isConnectable={isConnectable}
      />
      <img src={data.image.url} alt={data.image.alt} />
      <Handle
        type="source"
        position={Position.Right}
        id="a"
        isConnectable={isConnectable}
      />
    </>
  );
});

Working CodeSandbox: https://codesandbox.io/s/react-flow-image-node-img-nrt2xy?file=/ImageNode.js

Both examples produce the following output:

custom image node example output

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.