Closed nickreynolds closed 1 month ago
You're not importing the Node
typpe, but won't get an error because it's defined in DOM. Code below should fix your errors. Also origin
is set on the ReactFlow
component, not per-node. Finally, if you wanna go deep try doing your own state management, useNodesState
and useEdgesState
are not ideal for production.
Cheers
import { useCallback } from "react";
import {
Background,
BackgroundVariant,
Controls,
type Edge,
type Node,
type NodeOrigin,
ReactFlow,
addEdge,
useEdgesState,
useNodesState,
useReactFlow,
} from "@xyflow/react";
import MindMapEdge from "./mind-map-edge/mind-map-edge";
import SP123Node from "./mind-map-node/sp123-node";
const nodeTypes = {
sp123: SP123Node,
};
const edgeTypes = {
mindmap: MindMapEdge,
};
// this places the node origin in the center of a node
const nodeOrigin: NodeOrigin = [0.5, 0.5];
// we have to import the React Flow styles for it to work
import "@xyflow/react/dist/style.css";
import { nanoid } from "nanoid";
const initialNodes: Node[] = [];
const initialEdges: Edge[] = [];
export function MindmapForm() {
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
const { screenToFlowPosition } = useReactFlow();
const onConnect = useCallback(
(params) => setEdges((eds) => addEdge(params, eds)),
[setEdges]
);
const addSp123Node = (sp123ID: string) => {
const id = nanoid();
const newNode = {
id,
position: screenToFlowPosition({
x: 200,
y: 200,
}),
data: { sp123ID: sp123ID },
// origin: [0.5, 0.5],
type: "sp123",
};
setNodes([...nodes, newNode]);
};
return (
<div
className="w-5/6 h-5/6"
onPaste={(e) => {
const pastedText = e.clipboardData.getData("text");
addSp123Node(pastedText);
}}
>
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
nodeTypes={nodeTypes}
edgeTypes={edgeTypes}
nodeOrigin={nodeOrigin}
onConnect={onConnect}
fitView
>
<Controls showInteractive={false} />
<Background variant={BackgroundVariant.Dots} gap={12} size={1} />
</ReactFlow>
</div>
);
}
thanks so much for the quick reply @hsab !
With those changes, it fixed all but 1 of the errors (and I pushed those fixes to my repo):
const onConnect = useCallback(
(params) => setEdges((eds) => addEdge(params, eds)),
[setEdges]
);
is still giving Type error: Parameter 'params' implicitly has an 'any' type.
and trying to ignore it with @ts-ignore
doesn't work either (changing tsconfig strict
to false
fixes it but that's not ideal)
I'm able to run the app, however, but when I try to load it, I get this error:
src/app/mindmap/MindmapForm.tsx (36:58) @ initialNodes
⨯ TypeError: (0 , _xyflow_react__WEBPACK_IMPORTED_MODULE_5__.useNodesState) is not a function or its return value is not iterable
at MindmapForm (./src/app/mindmap/MindmapForm.tsx:36:106)
at stringify (<anonymous>)
at stringify (<anonymous>)
at stringify (<anonymous>)
digest: "2303245753"
34 |
35 | export function MindmapForm() {
> 36 | const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
| ^
37 | const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
38 | const { screenToFlowPosition } = useReactFlow();
Really appreciate your help. I also found the nextjs-reactflow
starter app and will try to see if I'm missing anything else that's in there. Will let you know if I'm still struggling though!
Also, should the quickstart documentation be updated to include BackgroundVariant.Dots
, or is that something that's only required in nextjs?
I was able to resolve all my issues by starting from the xyflow/react-flow-sample-apps/reactflow-nextjs
and then implementing the "add node on paste" functionality, instead of trying to start from scratch and follow different tutorials (which may be slightly outdated? or at least don't work as-is in my project)
I'll close this issue, thanks
What platform were you using when you found the bug?
@xyflow/react: ^12.3.1
Live code example
No response
Describe the Bug
I'm seeing multiple type errors when trying to implement the examples in the Quickstart Guide (https://reactflow.dev/learn) within a NextJS app. I've created a small repo that demonstrates the errors that you can look at here: https://github.com/nickreynolds/nextjs-xyflow-error
I've included details on the errors here
error with
useNodesState
:Fails to compile with the following error:
Lines error with
setEdges
:Gives a type error of:
error with
nds.concat(newNode)
:Gives an error of:
Error with
<Background variant="dots"...
:Gives an error of:
Steps to reproduce the bug or issue
pnpm i
)pnpm build
)Expected behavior
I would expect the simple example to compile correctly
Screenshots or Videos
No response
Additional context
No response