question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Side effects in useMemo

See original GitHub issue

First I just want to say great work with this!

I was reading through the code and noticed that you’re calling world.createRigidBody inside useMemo which breaks some React rules since world.createRigidBody has side effects. useMemo isn’t the best choice for this anyway because React can decide to throw away the value at any time and recompute it.

https://github.com/pmndrs/react-three-rapier/blob/a4b9f3fb2ca40abea8f5dcd6d4c44068f60fe398/packages/react-three-rapier/src/hooks.ts#L94-L116

In my own integration I ended up using a getter which as far as I can tell should be React 18+ safe. The approach is described in the bottom of https://github.com/reactwg/react-18/discussions/18

https://github.com/alexandernanberg/playground/blob/973c512aa192a6500a503a15aea6d6dd1b83d2be/src/components/physics.tsx#L265-L272

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
alexandernanbergcommented, May 24, 2022

with your permission of course

Sure, feel free to take anything that is useful 😄

Hm, supposedly we could initialize the rigidbody in useLayoutEffect to get around that…

I think I had to create both the rigidbody and collider in a useLayoutEffect to avoid “layout shift” (not sure what it’s called in a 3d context), was some time ago so don’t remember exactly why I had to do it that way

0reactions
alexandernanbergcommented, May 25, 2022

I’m still not convinced useMemo is the right choice here, since React can throw away the value at any time. A lazy state setter might be better (useState(() => createSomeApi(refGetter))), but technically it might still violate the React rules since you’re setting/getting the ref value in render (React 18 compatibility is hard 😅)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is okay to useMemo instead of useEffect for calling side-effect ...
You should not cause any side effect in useMemo . For more on this you can read here: https://reactjs.org/docs/hooks-reference.html#usememo.
Read more >
What's the big problem with (careful) side effects in useMemo?
The useMemo body does have side effects (it runs a "pause" which will be eventually followed by a "resume" when the render has...
Read more >
React.useMemo and when you should use it - everyday.codes
The only difference is that useEffect is intended for side-effects (hence the name), while functions in useMemo are supposed to be pure and ......
Read more >
дэн on Twitter: "@cakemakerjake useMemo() is not for side ...
useMemo () is not for side effects. It's for pure computation. Resetting state based on other state is here: reactjs.org. Hooks FAQ –...
Read more >
When Using useMemo is A Really Bad Idea | by Lina Suodyte
We should also not use useMemo when the function returns a primitive value, such as a boolean or a string. Because primitive values...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found