Use multiple useRefs in custom component #3117
-
This relates to the docs needing a bit of an upgrade to more common tasks. Its also that im super newb to react. OK now all good? No now I need logic objects in the component when I send it a method, in this example I debugger on signal() How much more hacky do I need to do to make this into a typical object store class. q&a: what am I trying to do. Simply and properly store values like I would in a class and use import React, { useRef, useState, forwardRef, useImperativeHandle, useEffect } from 'react';
import { useFrame } from '@react-three/fiber';
import { simplePhysics } from "../utilites/simplePhysics"
// in this example we need 2 refs
// one for internal so we can do animation stuff
// and the forwardref type so the parent component can work with it
//
// React internal does not allow array refs
// but does have an arcane useImperativeHandle
// which then does more and we are forced to put the functions into it
// it makes for some interesting new way to closure methods, but as per example
// its the only solution to the 2+ ref issue
function Ball(props, ref) {
const refsDerp = {
mesh : useRef(null),
physicsRef : useRef(new simplePhysics())
}
const meshRef = useRef(null);
const tacos = "skdmgldfg";
const physicsRef = useRef(new simplePhysics());
const ppp = useRef(9);
useImperativeHandle(ref, () => {
return {
fishness(ev){
signal()
}
};
}, []);
useFrame((state, delta) => {
// meshRef.current.rotation.x += delta;
// debugger
});
useEffect(()=>{
// debugger
});
function signal(){
debugger
// should be able to reach any const be it a string or a useframe from here
// meshRef.current.position.y += 0.1;
}
return (
<mesh
{...props}
ref={meshRef}
signal={signal}
>
<sphereGeometry args={[1, 12, 12]} />
<meshStandardMaterial color={hovered ? 'hotpink' : 'green'} />
</mesh>
);
}
export default forwardRef(Ball); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
DRUMROLLLLL |
Beta Was this translation helpful? Give feedback.
DRUMROLLLLL
You need to place a console.log(var) to get it to allow access
Will make a bug issue