React-Three-Fiber Canvas component causing 'Module parse failed: setter should have exactly one param' error in Next.js app: Solutions?

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

React-Three-Fiber Canvas component causing 'Module parse failed: setter should have exactly one param' error in Next.js app: Solutions?

问题

Canvas组件在我的Next.js应用中的react-three-fiber中不起作用。

我是react-three-fiber和Next.js的初学者,当我使用Canvas组件时,我遇到了以下错误:

./node_modules/three/build/three.module.js
Module parse failed: setter should have exactly one param (1433:13)
|         return this.source.data;
|     }
>     set image() {
|         let value = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : null;
|         this.source.data = value;

我的page.tsx文件:

"use client";

import { Canvas } from "@react-three/fiber";
import Box from "@/components/Box";

export default function Home() {
  return (
    <Canvas>
      <pointLight position={[10, 10, 10]} />
      <Box />
    </Canvas>
  );
}

Box.tsx:

import { useRef, useState } from "react";
import { useFrame, ThreeElements } from "@react-three/fiber";

export default function Box() {
  const ref = useRef<ThreeElements["mesh"]>(null!);
  const [hovered, hover] = useState(false);
  const [clicked, click] = useState(false);
  useFrame((state, delta) => (ref.current.rotation.x += delta));
  return (
    <mesh
      ref={ref}
      scale={clicked ? 1.5 : 1}
      onClick={(event) => click(!clicked)}
      onPointerOver={(event) => hover(true)}
      onPointerOut={(event) => hover(false)}
    >
      <boxGeometry args={[1, 1, 1]} />
      <meshStandardMaterial color={hovered ? "hotpink" : "orange"} />
    </mesh>
  );
}

我的next.config.js:

/** @type {import('next').NextConfig} */
const nextConfig = {
  transpilePackages: ["three"],
};

module.exports = nextConfig;

这是你需要的翻译,没有其他内容。

英文:

Canvas component in react-three-fiber not working in my next app.

Im beginner to react-three-fiber and next with app directory, and when i use Canvas component i get this error:

./node_modules/three/build/three.module.js
Module parse failed: setter should have exactly one param (1433:13)
|         return this.source.data;
|     }
&gt;     set image() {
|         let value = arguments.length &gt; 0 &amp;&amp; arguments[0] !== void 0 ? arguments[0] : null;
|         this.source.data = value;

my page.tsx file:

&quot;use client&quot;;

import { Canvas } from &quot;@react-three/fiber&quot;;
import Box from &quot;@/components/Box&quot;;

export default function Home() {
  return (
    &lt;Canvas&gt;
      &lt;pointLight position={[10, 10, 10]} /&gt;
      &lt;Box /&gt;
    &lt;/Canvas&gt;
  );
}

Box.tsx:

import { useRef, useState } from &quot;react&quot;;
import { useFrame, ThreeElements } from &quot;@react-three/fiber&quot;;

export default function Box() {
  const ref = useRef&lt;ThreeElements[&quot;mesh&quot;]&gt;(null!);
  const [hovered, hover] = useState(false);
  const [clicked, click] = useState(false);
  useFrame((state, delta) =&gt; (ref.current.rotation.x += delta));
  return (
    &lt;mesh
      ref={ref}
      scale={clicked ? 1.5 : 1}
      onClick={(event) =&gt; click(!clicked)}
      onPointerOver={(event) =&gt; hover(true)}
      onPointerOut={(event) =&gt; hover(false)}
    &gt;
      &lt;boxGeometry args={[1, 1, 1]} /&gt;
      &lt;meshStandardMaterial color={hovered ? &quot;hotpink&quot; : &quot;orange&quot;} /&gt;
    &lt;/mesh&gt;
  );
}

my next.config.js:

/** @type {import(&#39;next&#39;).NextConfig} */
const nextConfig = {
  transpilePackages: [&quot;three&quot;],
};

module.exports = nextConfig;

答案1

得分: 1

我最近遇到了这个问题。它与NextJS的转译器有关。我正在使用next@13.2.4进行工作。更新到最新版本,即next@13.4.4,问题得到了解决。

英文:

I came across this issue recently. It is related to NextJS's transpiler. I was working on next@13.2.4. Updated to the latest version i.e next@13.4.4 and the issue was resolved.

huangapple
  • 本文由 发表于 2023年5月20日 23:53:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296083.html
匿名

发表评论

匿名网友

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

确定