Answer the question
In order to leave comments, you need to log in
Forward Ref not working?
Good evening. Please help me figure out why when passing ref through HOC using forwardRef, I get null, when outputting this.documentRef to the console
@connect(
state => ({
// code...
}),
{
requestDocuments: someRequests
}
)
export class MyComponent extends React.Component {
// Code...
render() {
return (
<Grid ref={this.props.forwardRef} />
);
}
}
import React, {forwardRef} from 'react';
import {MyComponent } from './container';
export const MyComponentHOC = React.forwardRef((props, ref) => (
<MyComponent {...props} forwardRef={ref} />
));
import {MyComponentHOC} from './';
class StatefullMyComponentHOC extends React.Component {
state = {};
handleClick = (event) => {
console.log(this.documentRef);
};
documentRef = createRef();
render() {
return (
<Fragment>
<StatefullMyComponentHOC ref={this.documentRef} />
<button id="4" onClick={this.handleClick}>Обновить контейнер</button>
</Fragment>
);
}
}
Answer the question
In order to leave comments, you need to log in
Or rewrite Grid to forwardRef, similar to the example:
const Grid = React.forwardRef((props, ref) => (
<div ref={ref}>{props.children}</div>
));
export class MyComponent extends React.Component {
render() {
return (
<Grid innerRef={this.props.forwardRef} />
);
}
}
const Grid = props => (
<div ref={props.innerRef}>{props.children}</div>
);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question