react-three-renderer
react-three-renderer copied to clipboard
better way to access `scene` in children
I've had a few cases now where I've needed access to the scene reference to call methods on the THREE.scene object. I'm purposing adding this as a property to context. This would be similar to how react-redux handles passing store down the tree.
The current options for accessing this could be better. Let me know if I am missing better options. Currently, I see the following options.
-
Using
refsI can givescenearefand children arefand at the top of the react tree I can dothis.refs.scene.refs.customcomponent.refs.mesh -
Using
parentOn in the child component, I can crawl back up the tree doing.mesh.parent.parent.parent.someSceneMethod() -
Using
refsandpropsAt the top of the tree I can do can create a scene ref and pass it down as a prop to children.
I think using refs and props would be "the react way" of doing it, but you could also probably use a shared variable (e.g. require a file (that defines a simple container object) in multiple other files and access properties to set/get things like the scene
I can also think of a module to get and set custom data on objects but that would be quite complicated. From my tests using context can work too but if it doesn't it can be fixed!