New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: componentDidMountCalled before ref is set #28666
Labels
Status: Unconfirmed
A potential issue that we haven't yet confirmed as a bug
Comments
CodeMedic42
added
the
Status: Unconfirmed
A potential issue that we haven't yet confirmed as a bug
label
Mar 28, 2024
Added a couple of more Result: Further investigation is needed and maybe someone can explain if this is intentional behavior. Click for code w/ added debuggersimport React, { useRef, Component, forwardRef } from "react";
class Ex1 extends Component {
render() {
const { innerRef, children } = this.props;
debugger;
return (
<div
ref={(element) => {
console.log(
"This line is called after the componentDidMount in Ex2."
);
innerRef.current = element;
debugger;
}}
className="ex1"
>
{children}
</div>
);
}
}
class Ex2 extends Component {
componentDidMount() {
const { targetRef } = this.props;
console.log("This line is hit before targetRef is set.");
console.log(
"Also it appears to be called twice. Once before the ref is set and once after."
);
debugger;
}
render() {
const { innerRef } = this.props;
debugger;
return <div className="ex2">I do my own thing</div>;
}
}
const Bar1 = forwardRef((props, ref) => {
const { children } = props;
debugger;
return <Ex1 innerRef={ref}>{children}</Ex1>;
});
function Test() {
const targetRef = useRef();
debugger;
return (
<Bar1 ref={targetRef}>
<Ex2 targetRef={targetRef} />
</Bar1>
);
}
export default Test; |
|
I have somewhat similar conclusion to this. I guess this issue could be closed then @CodeMedic42? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have encountered an issue where a class componentDidMount method is called before a ref is set in another. Also I am seeing sometimes it is being called twice, but not always. The ref is set in a class called Ex1 and the componentDidMount in a class called Ex2. There is also a forwardRef function component in the mix.
React version: 18.2.0
Steps To Reproduce
Here is a fiddle with the issue. Please note that I was having issues with jsFiffle.net not fully loading. I don't think it was my fiddle, but it was acting weird. If that happens just create your own fiddle from scratch and it should work.
https://jsfiddle.net/CodeMedic42/d50mnjz8/4/
Here is a copy of my code
The current behavior
The componentDidMount method in Ex2 is called before, and sometimes after, the call to the ref function in Ex1.
The expected behavior
The componentDidMount method in Ex2 should be called once and after the ref is set in Ex1.
The text was updated successfully, but these errors were encountered: