Ref value is not accurate in nested mount
See original GitHub issueI was getting strange results from calling getBoundingClientRect()
on a ref value after mounting, the element’s height was always 0. However, when logging the value inside a timeout, it seems to return the actual height. Stranger still is this only happens in nested components.
Nested mount
import { createElement, mount } from 'forgo';
const List = () => {
let ref = {};
return {
mount: () => {
console.log(ref.value.getBoundingClientRect()); // 0
setTimeout(() => console.log(ref.value.getBoundingClientRect()), 0); // 927
},
render: () => (
<div style="overflow: auto; flex-grow: 1" ref={ref}>
<div style="position: relative; min-height: 0">
<div style="position: absolute">Hello world</div>
</div>
</div>
)
}
};
const App = () => {
return {
render: () => (
<div style="display: flex; flex-direction: column; min-height: 100vh">
<List />
</div>
)
};
};
mount(<App />, document.getElementById('mount'));
“Flat” mount
import { createElement, mount } from 'forgo';
const App = () => {
let ref = {};
return {
mount: () => {
console.log(ref.value.getBoundingClientRect());
setTimeout(() => console.log(ref.value.getBoundingClientRect()), 0);
},
render: () => (
<div style="display: flex; flex-direction: column; min-height: 100vh">
<div style="overflow: auto; flex-grow: 1" ref={ref}>
<div style="position: relative; min-height: 0">
<div style="position: absolute">Hello world</div>
</div>
</div>
</div>
)
};
};
mount(<App />, document.getElementById('mount'));
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
vue.js - Vue - access nested childs using ref - Stack Overflow
A way of performing parent/deep child, or deep ancestor/sibling communication, for simple scenarios, is using an event hub.
Read more >v7 register forward refs failed with nested components #4913
Describe the bug register forward refs failed while nesting components. Expected behavior ref to be created properly with the spread ...
Read more >A complete guide to React refs - LogRocket Blog
Learn how to use React refs, and why it's important to use them only when React can't handle a function call through its...
Read more >React Stripe.js reference | Stripe Documentation
The Stripe.js reference covers complete Elements customization details. ... to use Element components and access the Stripe object in any nested component.
Read more >Documentation - SolidJS · Reactive Javascript Library
They track a single value (which can be any JavaScript object) that ... This is useful for nested reactive scopes that you do...
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 FreeTop 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
Top GitHub Comments
I’ve adapted your code into a reproduction in a sandbox. It’s a different issue than #50.
The order of events looks like:
mount()
on Childstyle
attr on Parent’s elementsSince Child’s elements won’t be sized correctly without Parent actually being a flex container, you see the height
0
until forgo completes its render (i.e., until after thesetTimeout
completes).Forgo needs to be adjusted to call
syncStateAndProps()
prior to rendering child components. But because of the way forgo stores state or DOM elements, we might need to split up the state sync and props sync into separate steps.I started working on a failing unit test for this. JSDOM doesn’t set meaningful values in the return value for
getBoundingClientRect()
, but I think we can assert on the timing of the parent’s attr set (which is the root cause anyway).This has been released in Forgo v3.2.0.