Replies: 1 comment 4 replies
-
https://docs.pmnd.rs/react-three-fiber/advanced/pitfalls in principle you always go useFrame if you need to update something at high rate, always without exception. you don't setState in it because why would you? all it would cause is a needless back and forth. in useFrame you mutate the view, just like the frameloop in a three vanilla app would. it then does not matter where state comes from, if it's local or remote. if your state is remote (for instance a state manager) then you can access it throughout the component graph, but you don't bind components to it, you merely read from it in useFrame and apply it. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
This weekend I built a small practice project, I'm relatively familiar with React and Three separately and so have been interested in
react-three-fiber
. The project was a simulation, following a React style I decided to have a state object outside the render tree, that I would then pass in by props each time it changed. I had something like this:I updated the
SimulationData
using atick
method that went through and updated all the bacteria based off a time delta. It also handled other things like creating new bacteria, bouncing the bacteria when they went outside my stage, things like this.Then I had two main components
Sim
andBacteria
. Sim rendered a list of Bacteria in a group and Bacteria rendered a mesh.The approach I took was clearly wrong, but it went something like this in Sim:
I saw that the docs said not to call
setState
insideuseFrame
but I wanted to give it a go anyway, since this seemed the most intuitive. This performed really poorly. I'm guessing because my tree of components had to be fully re-rendered each frame, which had a bunch of overheads. I ended up taking this approach as it seemed intuitive to me as a good functional react style.To get it performing well I moved all the updating to be within each
Bacteria
component, and they directly updated their own group using a ref rather than using props to update theirposition
etc.The issue I ran into was that all the state information was trapped inside the
Bacteria
component, the parent component couldn't know about it. This made it difficult to operate on the whole simulation at once, making this harder to work with than a traditional scene graph.It seemed like
useFrame
was appropriate for managing updates when they are localised to just the current component, but when things need to sync across multiple components it didn't really fit anymore. I can see that there arereact-three-fiber
physics libraries - so this kind of thing must be possible! I just couldn't figure it out.So that brings me to my question:
What are best practices for managing frequent updates across a tree of objects?
Beta Was this translation helpful? Give feedback.
All reactions