fitView() on onLoad doesn't seem to act, but works with Control Panel
See original GitHub issueHello 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:
- Created 3 years ago
- Reactions:3
- Comments:11 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

This seems to happen when the elements are changed after ReactFlow gets mounted (e.g. with
useEffectlike 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 extrauseEffecthook. Minimal example: https://codesandbox.io/s/unruffled-ishizaka-fhljt?file=/src/App.jshappening to me too. Fixed it by adding setTimeout, i.e.
EDIT: this might be caused by setting ‘defaultPosition’, it doesn’t happen once I comment it out