let rdnRef = React.useRef<Rnd>(); const resetWidgetPosition = (): void => { const…
- User
let rdnRef = React.useRef(); const resetWidgetPosition = (): void => { const position = JSON.parse(localStorage.getItem('chatSDK_position')); const size = JSON.parse(localStorage.getItem('chatSDK_size')); if (position && rdnRef) { rdnRef.updatePosition(position); } if (size && rdnRef) { rdnRef.updateSize(size); } }; export declare class Rnd extends React.PureComponent<Props, State> { static defaultProps: DefaultProps; resizable: Resizable; draggable: TODO) => void; refResizable: (c: Resizable | null) => void; render(): JSX.Element; } I get a typescript error "Property 'updatePosition' does not exist on type 'MutableRefObject<Rnd | undefined>'." on .updatePosition and .updateSize. What is wrong and how do I fix it?
- Assistant
The error occurs because the type of
rdnRefisReact.MutableRefObject<Rnd | undefined>, which does not have theupdatePositionorupdateSizemethods. To fix this error, you can access the current property of therdnRefobject before calling the methods:const resetWidgetPosition = (): void => { const position = JSON.parse(localStorage.getItem('chatSDK_position')); const size = JSON.parse(localStorage.getItem('chatSDK_size')); if (position && rdnRef.current) { rdnRef.current.updatePosition(position); } if (size && rdnRef.current) { rdnRef.current.updateSize(size); } };By accessing the
currentproperty of therdnRefobject, you can access the actualRndcomponent instance and call itsupdateSizeandupdatePositionmethods without any TypeScript errors.