[React Flow]: The React Flow parent container needs a width and a height to render the graph. Help: https://reactflow.dev/error#004 이와 같은 에러가 계속 발생
1. width 값 설정
< 에러코드 >
import { useState, useCallback } from "react";
import ReactFlow, {
Controls,
Background,
applyNodeChanges,
applyEdgeChanges,
} from "reactflow";
import "reactflow/dist/style.css";
const initialNodes = [
{
id: "1",
data: { label: "Hello" },
position: { x: 0, y: 0 },
type: "input",
},
{
id: "2",
data: { label: "World" },
position: { x: 100, y: 100 },
},
];
const initialEdges = [
{ id: "1-2", source: "1", target: "2", label: "to the", type: "step" },
];
function App() {
const [nodes, setNodes] = useState(initialNodes);
const [edges, setEdges] = useState(initialEdges);
const onNodesChange = useCallback(
(changes) => setNodes((nds) => applyNodeChanges(changes, nds)),
[]
);
const onEdgesChange = useCallback(
(changes) => setEdges((eds) => applyEdgeChanges(changes, eds)),
[]
);
return (
<div style={{ height: "100%" }}>
<ReactFlow
nodes={nodes}
onNodesChange={onNodesChange}
edges={edges}
onEdgesChange={onEdgesChange}
fitView
>
<Background />
<Controls />
</ReactFlow>
</div>
);
}
export default App;
< 수정코드 >
import { useState, useCallback } from "react";
import ReactFlow, {
Controls,
Background,
applyNodeChanges,
applyEdgeChanges,
} from "reactflow";
import "reactflow/dist/style.css";
const initialNodes = [
{
id: "1",
data: { label: "Hello" },
position: { x: 0, y: 0 },
type: "input",
},
{
id: "2",
data: { label: "World" },
position: { x: 100, y: 100 },
},
];
const initialEdges = [
{ id: "1-2", source: "1", target: "2", label: "to the", type: "step" },
];
function App() {
const [nodes, setNodes] = useState(initialNodes);
const [edges, setEdges] = useState(initialEdges);
const onNodesChange = useCallback(
(changes) => setNodes((nds) => applyNodeChanges(changes, nds)),
[]
);
const onEdgesChange = useCallback(
(changes) => setEdges((eds) => applyEdgeChanges(changes, eds)),
[]
);
return (
<div style={{ width: "100vw", height: "100vh" }}> //width값 넣어주고 %가 아닌 vw,vh로 변경
<ReactFlow
nodes={nodes}
onNodesChange={onNodesChange}
edges={edges}
onEdgesChange={onEdgesChange}
fitView
>
<Background />
<Controls />
</ReactFlow>
</div>
);
}
export default App;
2. div 컴포넌트 유무
< 에러코드 >
import { useState } from "react";
import ReactFlow from "reactflow";
import "reactflow/dist/style.css";
const initialNodes = [
{
id: "1",
type: "input",
data: { label: "Input Node" },
position: { x: 250, y: 25 },
},
{
id: "2",
// you can also pass a React component as a label
data: { label: <div>Default Node</div> },
position: { x: 100, y: 125 },
},
{
id: "3",
type: "output",
data: { label: "Output Node" },
position: { x: 250, y: 250 },
},
];
const initialEdges = [
{ id: "e1-2", source: "1", target: "2" },
{ id: "e2-3", source: "2", target: "3", animated: true },
];
function Flow() {
const [nodes, setNodes] = useState(initialNodes);
const [edges, setEdges] = useState(initialEdges);
return <ReactFlow nodes={nodes} edges={edges} fitView />;
}
export default Flow;
< 수정코드 >
import React, { useState } from "react";
import ReactFlow from "reactflow";
import "reactflow/dist/style.css";
const initialNodes = [
{
id: "1",
type: "input",
data: { label: "Input Node" },
position: { x: 250, y: 25 },
},
{
id: "2",
// you can also pass a React component as a label
data: { label: <div>Default Node</div> },
position: { x: 100, y: 125 },
},
{
id: "3",
type: "output",
data: { label: "Output Node" },
position: { x: 250, y: 250 },
},
];
const initialEdges = [
{ id: "e1-2", source: "1", target: "2" },
{ id: "e2-3", source: "2", target: "3", animated: true },
];
function Flow() {
const [nodes, setNodes] = useState(initialNodes);
const [edges, setEdges] = useState(initialEdges);
return (
<div style={{ width: "100vw", height: "100vh" }}> //div로 감싸 width,height지정필요
<ReactFlow nodes={nodes} edges={edges} fitView />;
</div>
);
}
export default Flow;
'JavaScript[JS]' 카테고리의 다른 글
[ApexCharts.js] react에서 차트 활용하는 방법 (0) | 2024.04.25 |
---|---|
[ReactFlow] 코드 정리 및 해석 (0) | 2024.03.18 |
[React] React hook의 useCallback 이란? (0) | 2024.03.18 |
[React 공식문서 이해하기] Managing State (1) | 2024.02.13 |
[React] CRA (Create React App) 기초 정리 (0) | 2023.03.23 |
댓글