New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs need forwardRef example and a slightly more advanced real world usage example #3119
Comments
OK I worked it down a bit BUT its a SUPER UNKNOWN what does it break fix I can survive one extra namespace function App() {
const wutRefObj = useRef();
useFrame((_, delta) => {
if(wutRefObj){
wutRefObj.pointerLike.current.rotation.x += delta;
}
});
return (
<WutIsThis ref={wutRefObj} position={[-4, 0.4, 0]} scale={[2,1,1]} />
)
}
// this here, we could extend the base classes like normal
// with logics without having to spam the react component
// and with the hacky ref= below we can access this!!
class Wut extends Object3D{
isWut = true;
constructor(){
super();
}
tacos(){
console.log("tacos", this.position);
}
}
extend({ Wut })
function WutIsThis(props, ref) {
const datas = {
thisRef : useRef(),
}
// debugger
// ok so here we try ref =
// will never get the ref
// ref = datas.thisRef;
// but here if we point .current we get
// ref.current.current.position !!!
// so to better name the namespace we try adding a different name
ref.current = datas.thisRef;
ref.pointerLike = datas.thisRef;
useFrame((state, delta) => {
datas.thisRef.current.rotation.y += delta;
});
return (
<wut ref={datas.thisRef} {...props}>
<mesh>
<sphereGeometry args={[1, 12, 12]} />
<meshStandardMaterial color={'blue'} />
</mesh>
</wut>
);
}
export default forwardRef(WutIsThis); |
just to finish this for what I know. function WutIsThis222(props, ref) {
const datas = {
thisRef : useRef(),
tempRef : ref
}
useFrame((state, delta) => {
datas.thisRef.current.rotation.y += delta;
});
useEffect(() => {
datas.tempRef.current = datas.thisRef.current;
});
return (
<wut ref={datas.thisRef} {...props}>
<mesh receiveShadow castShadow >
<sphereGeometry args={[1, 12, 12]} />
<meshStandardMaterial color={'blue'} />
</mesh>
</wut>
);
}
export default forwardRef(WutIsThis222); |
Once you get past the simple demo tutorials for useRef and starting building a real app with nested models and logic, the complexity level skyrockets.
For one all custom components that will need a
useRef
in the parent require the component be built withforwardRef()
which at the very start is not at all obvious nor clear but its doable, but is no where within the docsextend()
looked like its a go to but I see that all that does is make react dom elements so you can do lowercase<customThing />
object3dsAfter that if you need a reference to the internal object with the ref, you can no longer just use a useRef AND a forward ref, you have to string together some other monster, this examples monster landed on
useImperativeHandle
which is not a well discussed hook in react from my poking aroundAnd once you use that it sets up a nested quagmire as you now have to assign any functions and members you need as it blanks out the ref.current.position for example. You can work around that and pass a pass of the ref to the object
But im going down a tunnel of klugery that should be as simple as
maObject.position.set()
and i am stuck looking at this nowplayerRef.current.mainObject.current.mainObject
and wondering what im doing wrong in reactness
anyhoo this is the basic example of what I have as a template as its evolving have no clue if its correct
On top of that, react docs mention useRef should be used sparingly but to do anything complex requires like most all of my objects have this template of sorts
The text was updated successfully, but these errors were encountered: