我正在使用 react-three-fiber 实现一个组件,如下所示:
const Controls = (props: any) => {
const controlsRef = useRef();
const { camera, gl } = useThree();
useFrame(() => controlsRef.current && controlsRef!.current!.update());
// ^ Errors with Object is possibly 'undefined'
useFrame(() => {
if (controlsRef !== undefined && controlsRef.current !== undefined) {
controlsRef.current.target = new THREE.Vector3(worldMap.length / 2 * CELL_WIDTH, 0, worldMap.length / 2 * CELL_WIDTH)
// ^ ALSO errors with Object is possibly undefined
}
})
return (
<orbitControls
{...props}
ref={controlsRef}
args={[camera, gl.domElement]}
enableRotate
enablePan={false}
maxDistance={100}
minDistance={5}
maxPolarAngle={Math.PI / 3}
/>
);
};
我试过添加:
if (controlsRef !== undefined && controlsRef.current !== undefined) {
controlsRef!.current!.target = ...
// Errors with target does not exist on type 'never'
}
也:
useFrame(() => controlsRef.current && controlsRef?.current?.update());
// Errors with update does not exist on type 'never'
唉,无济于事。我觉得我的头撞到了一堵无法移动的打字稿墙上!
我究竟做错了什么?
(如果需要可以创建代码沙箱)