xyflow / xyflow

React Flow | Svelte Flow - Powerful open source libraries for building node-based UIs with React (https://reactflow.dev) or Svelte (https://svelteflow.dev). Ready out-of-the-box and infinitely customizable.
https://xyflow.com
MIT License
22.06k stars 1.46k forks source link

Cannot pass component as a label. #4163

Closed leochoo closed 2 months ago

leochoo commented 2 months ago

Describe the Bug

My project is using React and TypeScript. I followed the example here .

I copied the code files exactly. I changed js extension to ts.

However, although it says you can also pass a React component as a label, I realized that in my project it breaks.

  {
    id: "2",
    // you can also pass a React component as a label
    data: { label: <div>Default Node</div> },
    position: { x: 100, y: 125 },
  },

Error: CleanShot 2024-04-16 at 17 47 57@2x CleanShot 2024-04-16 at 17 47 40

Reproduction of the bug

I cannot even reproduce this properly because it fails to recognize nodes and edges.ts on code sandbox somehow... but here is a good starting point.

(It seems like sandbox can sometimes reproduce, but not exactly) CleanShot 2024-04-16 at 17 49 45

https://codesandbox.io/p/sandbox/bug-react-flow-component-label-qvvfn3?file=%2Fsrc%2FApp.tsx%3A9%2C42

What else can i try to solve this issue?

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

Go to sandbox

Expected behavior

It should accept React component as a label for TypeScript.

Screenshots or Videos

No response

Platform

Additional context

No response

bcakmakoglu commented 2 months ago

Not sure where exactly you got that sandbox from but there's a couple things wrong with it.

  1. Missing styles for the app wrapper so the flow wouldn't show up even if there wasn't an error as you describe
  2. Trying to import from edges.ts when there's only a edges.js file
  3. Mixing tsx and js files (not necessarily wrong but for this sandbox demo why mix?)
  4. Putting the label into "" double quotes will not render a jsx element but a literal string

Here's the sandbox corrected and no errors: https://codesandbox.io/p/sandbox/bug-react-flow-component-label-forked-fphfmn?file=%2Fsrc%2Fnodes.js%3A13%2C43

leochoo commented 2 months ago

@bcakmakoglu It seems like my sandbox was not synced when you checked. It should all be TypeScript files now.

  1. Back to the original issue, you can see that the error is thrown when React component is passed to label.

CleanShot 2024-04-16 at 17 33 44

  1. The same problem persists as sandbox still fails to recognize nodes.ts and edges.ts.. but this is codesandbox issue right?
bcakmakoglu commented 2 months ago

Sandbox link is still the same one that I saw when I first opened it, so either you forgot to save changes or the link is wrong 😄

leochoo commented 2 months ago

@bcakmakoglu I updated the original question with updated screenshots! I also made sure the link is correct! Sorry for the trouble.

moklick commented 2 months ago

This is not an issue with React Flow. If you want to write JSX, you should use ".tsx" as a file extension instead of ".ts".

leochoo commented 2 months ago

Changing it to tsx and adding Node[] type indeed fixed the issue. thanks.