Unverified Commit c9bc101d authored by Pascal Pixel's avatar Pascal Pixel Committed by GitHub

[examples/with-three-js] Component with useLoader can use dynamic import (#14648)

The existing example gives an error on every hot reload, perhaps because of the way that an initial `useEffect` hook imports the `GLTFLoader` and it doesn't redo that on hot reload?

Putting the component using the `useLoader` hook in a dynamic import seems to fix this.

Open to better suggestions, this is kind of awkward to use still...
parent 1d0e7a8a
import { useRef, useState, useEffect } from 'react'
import * as THREE from 'three'
import { useFrame, useLoader } from 'react-three-fiber'
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader'
const Bird = ({ speed, factor, url, ...props }) => {
const gltf = useLoader(GLTFLoader, url)
const group = useRef()
const [mixer] = useState(() => new THREE.AnimationMixer())
useEffect(
() => void mixer.clipAction(gltf.animations[0], group.current).play(),
[gltf.animations, mixer]
)
useFrame((state, delta) => {
group.current.rotation.y +=
Math.sin((delta * factor) / 2) * Math.cos((delta * factor) / 2) * 1.5
mixer.update(delta * speed)
})
return (
<group ref={group}>
<scene name="Scene" {...props}>
<mesh
name="Object_0"
morphTargetDictionary={gltf.__$[1].morphTargetDictionary}
morphTargetInfluences={gltf.__$[1].morphTargetInfluences}
rotation={[1.5707964611537577, 0, 0]}
>
<bufferGeometry attach="geometry" {...gltf.__$[1].geometry} />
<meshStandardMaterial
attach="material"
{...gltf.__$[1].material}
name="Material_0_COLOR_0"
/>
</mesh>
</scene>
</group>
)
}
export default Bird
import { useRef, useState, useEffect, Suspense } from 'react'
import * as THREE from 'three'
import { Canvas, useFrame, useLoader } from 'react-three-fiber'
let GLTFLoader
const Bird = ({ speed, factor, url, ...props }) => {
const gltf = useLoader(GLTFLoader, url)
const group = useRef()
const [mixer] = useState(() => new THREE.AnimationMixer())
useEffect(
() => void mixer.clipAction(gltf.animations[0], group.current).play(),
[gltf.animations, mixer]
)
useFrame((state, delta) => {
group.current.rotation.y +=
Math.sin((delta * factor) / 2) * Math.cos((delta * factor) / 2) * 1.5
mixer.update(delta * speed)
})
return (
<group ref={group}>
<scene name="Scene" {...props}>
<mesh
name="Object_0"
morphTargetDictionary={gltf.__$[1].morphTargetDictionary}
morphTargetInfluences={gltf.__$[1].morphTargetInfluences}
rotation={[1.5707964611537577, 0, 0]}
>
<bufferGeometry attach="geometry" {...gltf.__$[1].geometry} />
<meshStandardMaterial
attach="material"
{...gltf.__$[1].material}
name="Material_0_COLOR_0"
/>
</mesh>
</scene>
</group>
)
}
import dynamic from 'next/dynamic'
import { Suspense } from 'react'
import { Canvas } from 'react-three-fiber'
const Bird = dynamic(() => import('../components/Bird'), { ssr: false })
const Birds = () => {
return new Array(5).fill().map((_, i) => {
......@@ -65,9 +30,6 @@ const Birds = () => {
}
const BirdsPage = (props) => {
useEffect(() => {
GLTFLoader = require('three/examples/jsm/loaders/GLTFLoader').GLTFLoader
}, [])
return (
<>
<Canvas camera={{ position: [0, 0, 35] }}>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment