繁体   English   中英

错误 R3F 钩子只能在 Canvas 组件中使用! 使用THREE函数

[英]Error R3F hooks can only be used within the Canvas component! UseTHREE function

我正在尝试根据鼠标移动旋转立方体,即使 <我的旋转框是画布的子项,我一直在得到:错误 R3F 钩子只能在画布组件中使用! 它可能是由 useTHREE 函数引起的,因为当我删除它时,错误消失了

import { Canvas, useFrame } from "react-three-fiber";
import "./styles.css";
import { useThree } from "@react-three/fiber";

const MyRotatingBox = (props) => {
  
  const mouse = useThree((state) => state.mouse)
  const myMesh = React.useRef();

  const [onHover, setOnHover] = useState(false);
  useFrame(({ clock }) => {
    
    if (onHover) {
      
      myMesh.current.rotation.x += props.mouse.movementX * 0.1;
    }
  });

  return (
    <mesh
      ref={myMesh}
      onPointerOver={(e) => setOnHover(true)}
      onPointerOut={(e) => setOnHover(false)}
    >
      <boxBufferGeometry />
      <meshPhongMaterial color="royalblue" />
    </mesh>
  );
}

export default function App() {
  return (
    <div className="App">
      <Canvas>
        <MyRotatingBox />
        <ambientLight intensity={0.1} />
        <directionalLight />
      </Canvas>
    </div>
  );
}

您正在混合命名空间。 正确的是晚了,@react-three/fiber

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM