English 中文(简体)
采用Ref hook、ref. 现行法可能没有界定。
原标题:React Fiber Mesh Element using useRef hook, ref.current is possibly undefined

Environment
Next JS, TypeScript, React Fiber

《刑法》

import { useFrame } from  @react-three/fiber 
import React, { useRef, useState } from  react 

interface PolyhedronCanvasProps {
    position: [number, number, number],
    polyhedron: any
}

const PolyhedronCanvas = ( props: PolyhedronCanvasProps ) => {
    const ref = useRef()     
    const [count, setCount] = useState(0)

    useFrame((_, delta) => {  
        ref.current.rotation.x += delta
        ref.current.rotation.y += 0.5 * delta
    })

    return (
        <mesh 
            position={props.position}
            ref={ref}            
            onPointerDown={() => setCount((count + 1) % 3)}
            geometry={props.polyhedron[count]}
        >
            <meshPhongMaterial color={ lime } wireframe/>
        </mesh>
    )
}

export default PolyhedronCanvas    
ref.current.rotation.x += delta
ref.current.rotation.y += 0.5 * delta

我也许可以操作下个小标题的申请,但我仍然沿用带<密码>的这两个字体的字典错误。 参看目前可能未定义的

我再一次能够管理该项目,但我想去除使我的jsx档案在《科索沃法典》中重复的字典错误。

最佳回答

规定<代码>ref的类型(见docs):

import { Mesh } from  three 

const ref = useRef<Mesh>(null)     

查询ref. Current 在分配价值观之前:

useFrame((_, delta) => {  
  if(ref.current) {
    ref.current.rotation.x += delta
    ref.current.rotation.y += 0.5 * delta
  }
})
问题回答
 import { Object3D } from "three";

 const ref = useRef();

 useFrame((_, delta) => {
    if (ref.current) {
      const object3D = ref.current as Object3D;
      object3D.rotation.x += delta;
      object3D.rotation.y += 0.5 * delta;
    }
  });




相关问题
store data in memory with nestjs

I am trying to persist some data in my nestjs server so I can then use that data in my client app through http requests. I have created a products.service.ts file with the function getAllData() that ...

React Hook Form Error on custom Input component

I am having a problem when I use react hook form + zod in my application, in short the inputs never change value and I get the following error in the console: Warning: Function components cannot be ...

Updatable promises using proxy in JavaScript

EDIT: I ve updated this question, and the old question is moved here. A POC can be found on this jsfiddle or the snippet below GOAL: The goal here is to make or rather simulate a promise that can be ...

热门标签