fix(useReactFlow): use onNodesChange and onEdgesChange
This commit is contained in:
@@ -33,7 +33,7 @@ const UseZoomPanHelperFlow = () => {
|
||||
const [nodes, setNodes, onNodesChange] = useNodesState(initialNodes);
|
||||
const [edges, setEdges, onEdgesChange] = useEdgesState(initialEdges);
|
||||
const onConnect = (params: Connection | Edge) => setEdges((eds) => addEdge(params, eds));
|
||||
const { project, setCenter, zoomIn, zoomOut, fitView } = useReactFlow();
|
||||
const { project, setCenter, zoomIn, zoomOut, fitView, addNodes, setNodes: setNodesHook } = useReactFlow();
|
||||
|
||||
const onPaneClick = useCallback(
|
||||
(evt) => {
|
||||
@@ -49,7 +49,7 @@ const UseZoomPanHelperFlow = () => {
|
||||
})
|
||||
);
|
||||
},
|
||||
[project]
|
||||
[project, setNodes]
|
||||
);
|
||||
|
||||
const onNodeClick = useCallback(
|
||||
@@ -60,6 +60,22 @@ const UseZoomPanHelperFlow = () => {
|
||||
[setCenter]
|
||||
);
|
||||
|
||||
const onAddNode = useCallback(() => {
|
||||
const newNode = {
|
||||
id: getId(),
|
||||
position: { x: Math.random() * 500, y: Math.random() * 500 },
|
||||
data: {
|
||||
label: 'New Node',
|
||||
},
|
||||
};
|
||||
|
||||
addNodes(newNode);
|
||||
}, [addNodes]);
|
||||
|
||||
const onResetNodes = useCallback(() => {
|
||||
setNodesHook(initialNodes);
|
||||
}, [setNodesHook]);
|
||||
|
||||
return (
|
||||
<ReactFlow
|
||||
nodes={nodes}
|
||||
@@ -77,6 +93,8 @@ const UseZoomPanHelperFlow = () => {
|
||||
<button onClick={() => zoomIn({ duration: 1200 })}>zoomIn</button>
|
||||
<button onClick={() => zoomOut({ duration: 0 })}>zoomOut</button>
|
||||
<button onClick={() => fitView({ duration: 1200, padding: 0.3 })}>fitView</button>
|
||||
<button onClick={onAddNode}>add node</button>
|
||||
<button onClick={onResetNodes}>reset nodes</button>
|
||||
</div>
|
||||
<Background />
|
||||
<MiniMap />
|
||||
+16
-18
@@ -31,11 +31,9 @@ import Undirectional from './Undirectional';
|
||||
import UpdatableEdge from './UpdatableEdge';
|
||||
import UpdateNode from './UpdateNode';
|
||||
import UseUpdateNodeInternals from './UseUpdateNodeInternals';
|
||||
import UseZoomPanHelper from './UseZoomPanHelper';
|
||||
import UseReactFlow from './UseReactFlow';
|
||||
import Validation from './Validation';
|
||||
|
||||
|
||||
|
||||
const routes = [
|
||||
{
|
||||
path: '/',
|
||||
@@ -146,8 +144,8 @@ const routes = [
|
||||
component: UpdateNode,
|
||||
},
|
||||
{
|
||||
path: '/usezoompanhelper',
|
||||
component: UseZoomPanHelper,
|
||||
path: '/usereactflow',
|
||||
component: UseReactFlow,
|
||||
},
|
||||
{
|
||||
path: '/useupdatenodeinternals',
|
||||
@@ -169,18 +167,18 @@ const Header = () => {
|
||||
const onChange = (event: ChangeEvent<HTMLSelectElement>) => navigate(event.target.value);
|
||||
|
||||
return (
|
||||
<header>
|
||||
<a className="logo" href="https://github.com/wbkd/react-flow">
|
||||
React Flow Dev
|
||||
</a>
|
||||
<select defaultValue={location.pathname} onChange={onChange}>
|
||||
{routes.map((route) => (
|
||||
<option value={route.path} key={route.path}>
|
||||
{route.path === '/' ? 'overview' : route.path.substring(1, route.path.length)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</header>
|
||||
<header>
|
||||
<a className="logo" href="https://github.com/wbkd/react-flow">
|
||||
React Flow Dev
|
||||
</a>
|
||||
<select defaultValue={location.pathname} onChange={onChange}>
|
||||
{routes.map((route) => (
|
||||
<option value={route.path} key={route.path}>
|
||||
{route.path === '/' ? 'overview' : route.path.substring(1, route.path.length)}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</header>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -189,7 +187,7 @@ ReactDOM.render(
|
||||
<Header />
|
||||
<Routes>
|
||||
{routes.map((route) => (
|
||||
<Route path={route.path} key={route.path} element={<route.component />}/>
|
||||
<Route path={route.path} key={route.path} element={<route.component />} />
|
||||
))}
|
||||
</Routes>
|
||||
</BrowserRouter>,
|
||||
|
||||
Reference in New Issue
Block a user