Skip to content

Latest commit

 

History

History
57 lines (46 loc) · 1.51 KB

File metadata and controls

57 lines (46 loc) · 1.51 KB
title The ReactFlowProvider component
sidebarTitle <ReactFlowProvider />

<ReactFlowProvider />

Source on GitHub

The <ReactFlowProvider /> component is a context provider that makes it possible to access a flow's internal state outside of the <ReactFlow /> component. Many of the hooks we provide rely on this component to work.

import { ReactFlow, ReactFlowProvider, useNodes } from '@xyflow/react'

export default function Flow() {
  return (
    <ReactFlowProvider>
      <ReactFlow nodes={...} edges={...} />
      <Sidebar />
    </ReactFlowProvider>
  )
}

function Sidebar() {
  // This hook will only work if the component it's used in is a child of a
  // <ReactFlowProvider />.
  const nodes = useNodes()

  return (
    <aside>
      {nodes.map((node) => (
        <div key={node.id}>
          Node {node.id} -
            x: {node.position.x.toFixed(2)},
            y: {node.position.y.toFixed(2)}
        </div>
      ))}
    </aside>
  )
}

Props

Notes

  • If you're using a router and want your flow's state to persist across routes, it's vital that you place the <ReactFlowProvider /> component outside of your router.
  • If you have multiple flows on the same page you will need to use a separate <ReactFlowProvider /> for each flow.