I'm coming from this thread: How to use React.forwardRef in a class based component? I also went through the docs quite carefully, but my ref forwarding simply doesn't seem to work.
Component to create ref:
class TextEditor extends Component {
constructor(props: Props) {
this.editor = createRef();
}
render() {
console.log(this.editor) // -> This is always empty
return (
<BaseEditor ref={this.editor}/>
);
}
}
Component to receive the ref:
class BaseEditor extends Component<Props> {
render() {
return (
<Editor ref={this.props.innerRef}/>
);
}
}
const BaseEditorForwardRef = React.forwardRef((props: any, ref: any) => (
<BaseEditor {...props} innerRef={ref} />
));
export default connect(mapStateToProps, mapDispatchToProps)(BaseEditorForwardRef)
I feel like everything is where it should. Yet the ref does not exist within TextEditor
if I'm forwarding the ref without using React.forwardRef
everything works.
Does somebody know what I'm doing wrong?