question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fitBounds doesn't get called on first render of graph, but it does in subsequent ones?

See original GitHub issue

hi there!

here’s my code below. essentially I’m wrapping my graph in this ReactFlowComponent and my expectation was that it would automatically “fit bounds” everytime it lays out the elements. however, oddly, this doesnt seem to work for the first render, but it works for the subsequent ones.

any ideas on how to make this work? I suspect it might be that fitBounds is a no-op if there is no ReactFlow component yet?

const ReactFlowComponent = ({
  elements,
  width,
  height,
}: {
  elements: Array<Node | Edge>;
  width: number | undefined;
  height: number | undefined;
}) => {
  // Fit the bounds of the laid-out graph to the size of the pane.
  const { fitBounds, fitView } = useZoomPanHelper();
  fitBounds({ x: 0, y: 0, width: width ?? 0, height: height ?? 0 });
  return (
    <ReactFlow
      elements={elements}
      nodeTypes={{
        nodeWithFormulaOnHover: NodeWithFormulaOnHover,
      }}
    />
  );
};

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
leejustincommented, Oct 19, 2021

I had this same problem and spent forever trying to figure it out. I tried adding it into onLoad as the person above me suggested but it didn’t work.

There was a recommendation buried somewhere deep in the issues in this repo that I tried out. I can’t take all the credit but I can’t find a reference to it at the moment. Here’s a snippet of what worked for me:

  const [reactFlowInstance, setReactFlowInstance] = useState(null);

  const onLoadHandler = (rf) => {
    setReactFlowInstance(rf);
  }

  useEffect(() => {
    if (reactFlowInstance) {
        reactFlowInstance.fitView();
    }
  }, [reactFlowInstance]);

  // The only relevant part below is that `onLoad` uses `onLoadHandler`
  return (
        <ReactFlow
          elements={elements}
          onLoad={onLoadHandler}
          defaultZoom={1.2}
          onElementClick={handleNodeSelection}
          snapToGrid={true}
          snapGrid={snapGrid}
          nodesDraggable={false}
          nodeExtent={nodeExtent}
        >
        </ReactFlow>
  )

This is a simplified version of what I have, but it’s pretty straightforward. This is a second useEffect on top of another one that I have which does something else.

0reactions
moklickcommented, Oct 28, 2021

Thanks @datoslabs and @leejustin 😃 we will make it easier in v10 but for now this is the way to do it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mapbox fitBounds, function bounds map but does not render ...
Map bounds are get fit according to geojson, but problem is that when there is any marker on map and I try to...
Read more >
Folium 0.12.1 documentation
Creates an Icon object that will be rendered using Leaflet.awesome-markers. Parameters. color (str, default 'blue') –. The color of the marker. You can...
Read more >
Trimble Maps WebGL API Documentation
The Map object represents the map on your page. It exposes methods and properties that enable you to programmatically change the map, and...
Read more >
Maps JavaScript API | Google Developers
Returns the lat/lng bounds of the current viewport. If more than one copy of the world is visible, the bounds range in longitude...
Read more >
tomchentw/react-google-maps - Gitter
@tomchentw i'll fix first note then you can merge it ... @yhsiang Not sure if it's good paradigm to expose public function ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found