A
A
alex4answ2021-01-22 19:31:10
typescript
alex4answ, 2021-01-22 19:31:10

How to use forwardRef on class bean?

Good afternoon, I need a ref on a child component that is at the 2nd nesting level, I use forwardRef.

Parent of the component to be referenced:

interface BProps {
  title: string;
  innerRef?: React.Ref<CComponent>;
}

class BComponent extends React.Component<BProps> {
  render() {
    const {innerRef, ...props} = this.props;
    return <CComponent {...props} ref={innerRef} />;
  }
}

export default React.forwardRef<CComponent, BProps>((props, ref) => (
  <BComponent
  {...props}
  innerRef={ref} 
  />
));


But now I can't pass props.childrenwrites that chilren is not expected in component props.
I had to hack and add to BProps children?: React.ReactNode
And everything works , the ref is passed, everything is as it should, but the feeling that this is hack-work does not leave me , and somewhere I didn’t do something right.

Can you please tell me how to correctly use forwardRef with class components in typescript?
Those examples that I found, they do not fit, maybe ref to normal html elements, and without specifying props and so on.

What's my mistake?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Gurtovoy, 2021-01-26
@jt3k

// @ts-ignore didn't help ?
Maybe any will help

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question