Answer the question
In order to leave comments, you need to log in
Why in production React in ref.current returns a DOM element and not an object?
There is a certain component "A", like this:
class ComponentA extends React.Component {
someMethod = () => {
console.log('test');
};
render() {
return (
<div className="component-a" ref={this.props.forwardedRef}>
...
</div>
);
}
}
export default React.forwardRef((props, ref) => {
return <ComponentA {...props} forwardedRef={ref} />;
});
import ComponentA from "./ComponentA";
class ComponentB extends React.Component {
constructor(props) {
this.someRef = React.createRef();
}
render() {
return (
<div className="component-b">
<ComponentA ref={this.someRef} />
<button onClick={() => this.someRef.current.someMethod()}>Click!</button>
</div>
);
}
}
this.someRef.current.someMethod is not a function
Answer the question
In order to leave comments, you need to log in
Since a ref is a reference DOM element and it has properties and methods .
PS Read more about refs in the documentation
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question