lanyard-home was forked from 3d home page setup
Continue chatting to ask questions about or make changes to it.
I understand the error you're encountering. The issue is likely caused by trying to access ref.current
before it has been initialized. Let's modify the Band
component to address this error. We'll use the useRef
hook to create an array of refs for each point, and then use these refs in the RopeJoint
components.
Here's the revised Lanyard.tsx
file:
Here are the key changes made to address the error:
We've created an array of refs using useRef
in the Band
component:
const refs = useRef<any[]>([])
We initialize this array of refs in the useEffect
hook:
refs.current = newPoints.map(() => ({ current: null }))
We've updated the RigidBody
components to use these refs:
<RigidBody
key={index}
position={point}
ref={(el) => (refs.current[index] = el)}
...
>
In the RopeJoint
components, we now use these refs directly:
<RopeJoint
key={`rope-${index}`}
bodyA={refs.current[index - 1]}
bodyB={refs.current[index]}
length={spacing}
/>
We've also simplified the RopeJoint
component to use the useRopeJoint
hook directly, without needing to pass pivotA
and pivotB
:
const RopeJoint = ({
bodyA,
bodyB,
length,
}: {
bodyA: any
bodyB: any
length: number
}) => {
useRopeJoint(bodyA, bodyB, [
[0, 0, 0],
[0, 0, 0],
{
length,
damping: 0.1,
stiffness: 100,
},
])
return null
}
These changes should resolve the "ref.current is null" error by ensuring that we're not trying to access ref.current
before it's been initialized. The RigidBody
components are now properly referenced, and the RopeJoint
components use these references correctly.