You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I come across one strange phenomenon while using react-reflex. When we set a ref for a <div> that nested inside a <ReflexElement>, at the first rendering, even within the useEffect, my ref is null. I bet I miss something here since this is quite a common use case: e.g. I grab the ref so I can focus on an <input> or do some dimension measuring, etc.
How to reproduce the issue?
Please see the snippet below (I took the basic example in README):
constApp=(props: {})=>{constref=useRef<HTMLDivElement>(null);console.log("ref",ref);useEffect(()=>{console.log(ref.current);// this will be null},[]);return(<ReflexContainerorientation="vertical"><ReflexElementclassName="left-pane"><divref={ref}className="pane-content">LeftPane(resizeable)</div></ReflexElement><ReflexSplitter/><ReflexElementclassName="right-pane"><divclassName="pane-content">RightPane(resizeable)</div></ReflexElement></ReflexContainer>);};
I would expect in the useEffect block, my ref is not null.
Actual behavior
My ref is null.
Additional context
Now, if I do some resizing and use onResize on <ReflexElement>, and then I console.log the ref, I can actually see the ref being populated. I'm really puzzled by this and I bet I might have missed something. Could you kindly give me some guidance here?
Thank you so much!
If instead, I turn the nested content into another component, then it's fine though. I guess this is the workaround, but I still don't understand the behaviour mentioned above.
constSubComp: React.FC<{}>=()=>{constref=useRef<HTMLDivElement>(null);console.log("ref",ref);useEffect(()=>{console.log(ref.current);// this will NOT be null},[]);return(<divref={ref}className="pane-content">LeftPane(resizeable)</div>);};constApp=(props: {})=>{return(<ReflexContainerorientation="vertical"><ReflexElementclassName="left-pane"><SubComp/></ReflexElement><ReflexSplitter/><ReflexElementclassName="right-pane"><divclassName="pane-content">RightPane(resizeable)</div></ReflexElement></ReflexContainer>);};
The text was updated successfully, but these errors were encountered:
akphi
changed the title
React ref of element inside <ReflexElement> is not properly on first render
React ref of element inside <ReflexElement> is not properly on first render
Jul 15, 2021
Description
I come across one strange phenomenon while using
react-reflex
. When we set aref
for a<div>
that nested inside a<ReflexElement>
, at the first rendering, even within theuseEffect
, myref
isnull
. I bet I miss something here since this is quite a common use case: e.g. I grab theref
so I can focus on an<input>
or do some dimension measuring, etc.How to reproduce the issue?
Please see the snippet below (I took the basic example in README):
The full code can be found at: https://github.com/akphi/issue-repo/tree/react-reflex-no-ref
Expected behavior
I would expect in the
useEffect
block, myref
is notnull
.Actual behavior
My
ref
is null.Additional context
Now, if I do some resizing and use
onResize
on<ReflexElement>
, and then Iconsole.log
theref
, I can actually see theref
being populated. I'm really puzzled by this and I bet I might have missed something. Could you kindly give me some guidance here?Thank you so much!
If instead, I turn the nested content into another component, then it's fine though. I guess this is the workaround, but I still don't understand the behaviour mentioned above.
The text was updated successfully, but these errors were encountered: