Skip to content

Is using state to manipulate the Object3D a good idea? #1288

Answered by drcmda
JBudny asked this question in Q&A
Discussion options

You must be logged in to vote

this one should answer it: https://docs.pmnd.rs/react-three-fiber/advanced/pitfalls

the tldr is, flags yes, fast updates no. just like plain react, you would never animate a div across the screen with setState but you can set props on it, colors, text, things that happen occasionally.

with zustand you can tie components to state without causing render, useFrame would then write updates to the object3d.

this especially makes no sense:

  const [rotation, setRotation] = useState(0)
  useFrame(({ clock }) => setRotation(() => Math.sin(clock.getElapsedTime())))

you are pumping 60 component updates per second through react, 60 times per second where it has to diff for changes, execute all hooks…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@JBudny
Comment options

Answer selected by JBudny
Comment options

You must be logged in to vote
1 reply
@Jbmanllr
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants
Converted from issue

This discussion was converted from issue #1287 on April 27, 2021 13:59.