R3F: Hooks can only be used within the Canvas component

huangapple go评论105阅读模式
英文:

R3F: Hooks can only be used within the Canvas component

问题

我正在尝试使用ThreeJs(我在使用React)制作一个带有动画的立方体,上面有一个“hello”文本。它显示“Hooks只能在Canvas组件内使用!”,但我已经在我的代码中添加了Canvas组件。这是代码。我做错了什么?(附言:我是React和ThreeJS的新手)

  1. import React, { useRef } from "react";
  2. import { PerspectiveCamera, RenderTexture, Text } from "@react-three/drei";
  3. import { Canvas, useFrame } from "@react-three/fiber";
  4. import styled from "styled-components";
  5. const Container = styled.div`
  6. height: 100vh;
  7. width: 100%;
  8. scroll-snap-align: center;
  9. `
  10. const Test = () => {
  11. const textRef = useRef();
  12. useFrame(
  13. (state) =>
  14. (textRef.current.position.x = Math.sin(state.clock.elapsedTime) * 2)
  15. );
  16. return (
  17. <Container>
  18. <Canvas>
  19. <mesh>
  20. <boxGeometry />
  21. <meshStandardMaterial>
  22. <RenderTexture attach="map">
  23. <PerspectiveCamera makeDefault position={[0, 0, 5]} />
  24. <color attach="background" args={["#dc9dcd"]} />
  25. <Text ref={textRef} fontSize={3} color="#555">
  26. hello
  27. </Text>
  28. </RenderTexture>
  29. </meshStandardMaterial>
  30. </mesh>
  31. </Canvas>
  32. </Container>
  33. );
  34. };
  35. export default Test;

如果您需要更多帮助,请告诉我。

英文:

I am trying to make a Cube with an animated "hello" text on it using threeJs (im using react).
It says "Hooks can only be used within the Canvas component!" but I even added the Canvas component in my code. R3F: Hooks can only be used within the Canvas component

This is the code. What am I doing wrong ? (PS: I am new to React and ThreeJS)

  1. import React, { useRef } from &quot;react&quot;;
  2. import { PerspectiveCamera, RenderTexture, Text } from &quot;@react-three/drei&quot;;
  3. import { Canvas, useFrame } from &quot;@react-three/fiber&quot;;
  4. import styled from &quot;styled-components&quot;;
  5. const Container = styled.div`
  6. height: 100vh;
  7. width: 100%;
  8. scroll-snap-align: center;
  9. `
  10. const Test = () =&gt; {
  11. const textRef = useRef();
  12. useFrame(
  13. (state) =&gt;
  14. (textRef.current.position.x = Math.sin(state.clock.elapsedTime) * 2)
  15. );
  16. return (
  17. &lt;Container&gt;
  18. &lt;Canvas&gt;
  19. &lt;mesh&gt;
  20. &lt;boxGeometry /&gt;
  21. &lt;meshStandardMaterial&gt;
  22. &lt;RenderTexture attach=&quot;map&quot;&gt;
  23. &lt;PerspectiveCamera makeDefault position={[0, 0, 5]} /&gt;
  24. &lt;color attach=&quot;background&quot; args={[&quot;#dc9dcd&quot;]} /&gt;
  25. &lt;Text ref={textRef} fontSize={3} color=&quot;#555&quot;&gt;
  26. hello
  27. &lt;/Text&gt;
  28. &lt;/RenderTexture&gt;
  29. &lt;/meshStandardMaterial&gt;
  30. &lt;/mesh&gt;
  31. &lt;/Canvas&gt;
  32. &lt;/Container&gt;
  33. );
  34. };
  35. export default Test;

答案1

得分: 0

The error message suggests that you are currently using a react-three-fiber hook outside of the Canvas component. To resolve this, you can encapsulate your text cube in its own component and use useFrame there. Here's the translated code:

  1. const TextCube = () => {
  2. const textRef = useRef();
  3. useFrame(
  4. (state) =>
  5. (textRef.current.position.x = Math.sin(state.clock.elapsedTime) * 2)
  6. );
  7. return (
  8. <mesh>
  9. <boxGeometry />
  10. <meshStandardMaterial>
  11. <RenderTexture attach="map">
  12. <PerspectiveCamera makeDefault position={[0, 0, 5]} />
  13. <color attach="background" args={["#dc9dcd"]} />
  14. <Text ref={textRef} fontSize={3} color="#555">
  15. hello
  16. </Text>
  17. </RenderTexture>
  18. </meshStandardMaterial>
  19. </mesh>
  20. );
  21. };

Then, use it in your Test component like this:

  1. const Test = () => {
  2. return (
  3. <Container>
  4. <Canvas>
  5. <TextCube />
  6. </Canvas>
  7. </Container>
  8. );
  9. };

This should do the trick!

英文:

As the error says, you are currently calling a react-three-fiber hook not within the Canvas component. You could wrap your text cube in its own component and call useFrame there:

  1. const TextCube = () =&gt; {
  2. const textRef = useRef();
  3. useFrame(
  4. (state) =&gt;
  5. (textRef.current.position.x = Math.sin(state.clock.elapsedTime) * 2)
  6. );
  7. return (
  8. &lt;mesh&gt;
  9. &lt;boxGeometry /&gt;
  10. &lt;meshStandardMaterial&gt;
  11. &lt;RenderTexture attach=&quot;map&quot;&gt;
  12. &lt;PerspectiveCamera makeDefault position={[0, 0, 5]} /&gt;
  13. &lt;color attach=&quot;background&quot; args={[&quot;#dc9dcd&quot;]} /&gt;
  14. &lt;Text ref={textRef} fontSize={3} color=&quot;#555&quot;&gt;
  15. hello
  16. &lt;/Text&gt;
  17. &lt;/RenderTexture&gt;
  18. &lt;/meshStandardMaterial&gt;
  19. &lt;/mesh&gt;
  20. );
  21. };

Then use it in your Test component like this:

  1. const Test = () =&gt; {
  2. return (
  3. &lt;Container&gt;
  4. &lt;Canvas&gt;
  5. &lt;TextCube /&gt;
  6. &lt;/Canvas&gt;
  7. &lt;/Container&gt;
  8. );
  9. };

This should to the trick!

huangapple
  • 本文由 发表于 2023年4月19日 16:57:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052578.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定