Open arswaw opened 9 months ago
I am experiencing the same issue with "@react-three/fiber": "^8.15.19"
.
I think the type we use in R3F is wrong and should allow undefined. We can align with the DOM types on this.
I think this might be the correct behavior, you'll get the same error with a regular DOM element. You need to type your ref like this and init with null. (React will set ref.current = null
when the component unmounts)
export const Component = () => {
const ref = useRef<Mesh>(null);
return <mesh ref={ref} />;
};
Respectively, this is how you would type a ref to a div
export const Component = () => {
const ref = useRef<HTMLDivElement>(null);
return <div ref={ref} />;
};
This will be fixed with React 19. Looks like this was an upstream issue.
Discussed in https://github.com/pmndrs/react-three-fiber/discussions/3121
; children?: React.ReactNode; ref?: React.Ref;
key?: React.Key;
onUpdate?: (self: T) => void;
}
```
If ref (and perhaps related attributes) were typed:
`ref?: React.Ref`
then it would solve the problem.
When I modified the R3F dependency directly, the error disappeared.
Since that is bad practice, I have used the temporary solution:
`const cubeRef = useRef() as MutableRefObject`
However this decreases type safety since the ref could return undefined at runtime.
I am not sure if I am in misinterpreting the problem or the solution. Does my new union type make sense?