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.

fitView() on onLoad doesn't seem to act, but works with Control Panel

See original GitHub issue

Hello there,

I am currently working on the fitView() method on my diagram, but I see that it doesn’t seem to fit on onLoad().

I am doing this, following the doc :

const onLoad = (reactFlowInstance) => {
    reactFlowInstance.fitView();
}

But it doesn’t happen. But when I click on the fitView button on the control, it does work !

Here is how I use the props in the component :

<ReactFlow
        elements={elements}
        elementsSelectable={diagramMode !== "View"}
        onLoad={onLoad}
        nodesDraggable={nodesDraggableState}
        paneMoveable
        zoomOnScroll
        arrowHeadColor="#206584"
        nodesConnectable={nodesConnectableState}
        defaultPosition={[0, 0]}
        defaultZoom={1}
        onNodeDragStop={onNodeDragStop}
        onConnect={onConnect}
        onElementClick={openForm}
        onElementsRemove={onElementsRemove}
        deleteKeyCode={46}
      >
        <MiniMap
          nodeColor={(node) => {
            switch (node.type) {
              case "input":
                return "red";
              case "default":
                return "#00ff00";
              case "output":
                return "rgb(0,0,255)";
              default:
                return "#eee";
            }
          }}
        />
        <Controls />
      </ReactFlow>

I tried adding padding as a parameter, to do as the Control Panel . Here is the code of the control panel I found in the sources :

{showFitView && (
        <div
          className="react-flow__controls-button react-flow__controls-fitview"
          onClick={() => fitView({ padding: 0.1 })}
        >
          <FitviewIcon />
        </div>
      )}

But even when adding padding like this, it does not change the behaviour. I tried to look at the older issues but couln’t find something related.

Would you have any idea about the source of this ?

Thank you, Yoann

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

16reactions
chrtzecommented, Nov 24, 2020

This seems to happen when the elements are changed after ReactFlow gets mounted (e.g. with useEffect like in the custom node example). That is because the onLoad handler gets called before the new elements are passed to the component. You can workaround this by waiting for the elements to change in an extra useEffect hook. Minimal example: https://codesandbox.io/s/unruffled-ishizaka-fhljt?file=/src/App.js

7reactions
KutnerUricommented, Sep 29, 2020

happening to me too. Fixed it by adding setTimeout, i.e.

onLoad={(instance) => setTimeout(() => instance.fitView(), 0)}

EDIT: this might be caused by setting ‘defaultPosition’, it doesn’t happen once I comment it out

Read more comments on GitHub >

github_iconTop Results From Across the Web

My onload listener doesn't seem to be working - Stack Overflow
I have a simple init() function tied to window.onload . init() is firing but I get a weird error such as that it...
Read more >
A react library for building node-based graphs - Morioh
React Flow is a library for building node-based graphs. You can easily implement custom node types and it comes with components like a...
Read more >
HTML onload Event Attribute - W3Schools
The onload attribute can be used to check the visitor's browser type and browser version, and load the proper version of the web...
Read more >
EnableAdaptiveUI onLoad event | ASP.NET MVC - EJ 2 Forums
Hi, i'm trying to dynamically set EnableAdaptiveUI onLoad event, but doesn't seems to do any change. Only works when i set the property ......
Read more >
Onload not firing on Chrome for Windows | WordPress.org
And: the backup of my site on another server always works in Chrome, ... and I seem to remember that it ran OK...
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