英文:
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;
| }
> set image() {
| let value = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : null;
| this.source.data = value;
my page.tsx file:
"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>
);
}
my next.config.js:
/** @type {import('next').NextConfig} */
const nextConfig = {
transpilePackages: ["three"],
};
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论