Giter VIP home page Giter VIP logo

Comments (3)

CodyJasonBennett avatar CodyJasonBennett commented on June 11, 2024

Looking into this, why not eagerly create World here in state?

const worldRef = useRef<World>();
const getWorldRef = useRef(() => {
if (!worldRef.current) {
const world = new rapier.World(vectorArrayToVector3(gravity));
worldRef.current = world;
}
return worldRef.current;
});

I also see this in other places, although this is initialized immediately in createWorldApi before passing in context.

from react-three-rapier.

wiledal avatar wiledal commented on June 11, 2024

Good call! I'll wrap the Demos in StrictMode from now on.

There is some work to be done on RigidBody lifecycles.
React 18 Strict Mode has some annoying but necessary behaviors in order to catch potential problems, especially in Development Mode where it runs all components twice, invoking effects and state-setters both times.

The safe way to ensure a single eagerly created instance is to use the refGetter pattern like above ☝️ @CodyJasonBennett -- I really dislike it, but what can you do.

It would be nice to create the world in a useState, but then we'd have a non-symmetrical side-effect where we have to destroy it in an unrelated useEffect.

I should be passing the refGetter to the update hooks in the RigidBody instead of the refs. Strict Mode is causing the component to mount multiple times, breaking the rigid body reference in the "newest" world...

Do you have suggestions on cleaner ways of handling this?

from react-three-rapier.

CodyJasonBennett avatar CodyJasonBennett commented on June 11, 2024

In this case, you would nullify worldRef.current on unmount to unsatisfy the null check, but I wanted to understand why internals were designed that way and if they work around existing problems. Do you need access to these bodies at render? Can we remove render-effects entirely?

from react-three-rapier.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.