I tried to make meshes interact with on click using react-spring and when I build this onclick function I got this error
type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Check your code at App.js:27.
and App.js code follows
import "./App.scss";
import { OrbitControls, Plane, softShadows, Sphere } from "@react-three/drei";
// import SpinningMesh from "./Box";
import { Canvas, useFrame } from "@react-three/fiber";
import { useRef, useState } from "react";
import { MeshWobbleMaterial } from "@react-three/drei";
import { useSpring, a } from "react-spring";
// soft Shadows
softShadows();
const SpinningMesh = ({ position, color, speed, args }) => {
//ref to target the mesh
const mesh = useRef();
//useFrame allows us to re-render/update rotation on each frame
useFrame(() => (mesh.current.rotation.x = mesh.current.rotation.y += 0.01));
//Basic expand state
const [expand, setExpand] = useState(false);
// React spring expand animation
const props = useSpring({
scale: expand ? [1.4, 1.4, 1.4] : [1, 1, 1],
});
return (
<a.mesh
position={position}
ref={mesh}
onClick={() => setExpand(!expand)}
scale={props.scale}
castShadow
>
<boxBufferGeometry attach="geometry" args={args} />
<MeshWobbleMaterial
color={color}
speed={speed}
attach="material"
factor={0.6}
/>
</a.mesh>
);
};
const App = () => {
return (
<>
{/* <Header /> */}
{/* Our Scene & Camera is already built into our canvas */}
<Canvas
colorManagement
shadowMap
camera={{ position: [-5, 2, 10], fov: 60 }}
>
{/* This light makes things look pretty */}
<ambientLight intensity={0.3} />
{/* Our main source of light, also casting our shadow */}
<directionalLight
castShadow
position={[0, 10, 0]}
intensity={1.5}
shadow-mapSize-width={1024}
shadow-mapSize-height={1024}
shadow-camera-far={50}
shadow-camera-left={-10}
shadow-camera-right={10}
shadow-camera-top={10}
shadow-camera-bottom={-10}
/>
{/* A light to help illumnate the spinning boxes */}
<pointLight position={[-10, 0, -20]} intensity={0.5} />
<pointLight position={[0, -10, 0]} intensity={1.5} />
<group>
{/* This mesh is the plane (The floor) */}
<mesh
rotation={[-Math.PI / 2, 0, 0]}
position={[0, -3, 0]}
receiveShadow
>
<planeBufferGeometry attach="geometry" args={[100, 100]} />
<shadowMaterial attach="material" opacity={0.3} />
</mesh>
<SpinningMesh
position={[0, 1, 0]}
color="lightblue"
args={[3, 2, 1]}
speed={2}
/>
<SpinningMesh position={[-2, 1, -5]} color="pink" speed={6} />
<SpinningMesh position={[5, 1, -2]} color="pink" speed={6} />
</group>
{/* Allows us to move the canvas around for different prespectives */}
<OrbitControls />
</Canvas>
</>
);
};
export default App;
can someone help me to solve this problem