I am trying to add ref to my custom function component. What is the difference between forwardRef and ComponentPropsWithRef?
const MyComponent = (ref) => { return <div ref={ref}>Some text</div> }
I have tried to use ComponentPropsWithRef but it doesn’t add ref at all
>Solution :
ref.current without React.forwardRef will always be whatever initial value was passed to useRef and won’t change.
React.ComponentPropsWithRef only adds ref property to the props of the component.
The solution might be const MyComponent= forwardRef<HTMLDivElement, MyComponentProps>((ref, props) => {return <div ref={ref}>Some text</div>})