react-three-fiber icon indicating copy to clipboard operation
react-three-fiber copied to clipboard

Primitive with children mounting / unmounting + documentation

Open AlaricBaraou opened this issue 1 year ago • 4 comments

The documentation says the following about primitives

You can still give it properties or attach nodes to it. [...] Primitives will not dispose of the object they carry on unmount, you are responsible for disposing of it!

And in the source code it says

Primitives should not have objects and children that are attached to them declaratively ...

The code comment seems to be clear about how primitives shouldn't have any children declaratively while the documentation says we can add nodes to it.

It might be me misunderstanding but they seem to contradict each other and it seems that adding children to primitives isn't really supported.

I made some test to check it out.

Unmount primitive parent: expect children to be removed from the scene ✔ https://codesandbox.io/p/sandbox/elegant-kepler-grc767?file=%2Fsrc%2FApp.js%3A39%2C31 Change primitive key: expect children to be removed from the scene before being re-created or reused❌( they're recreated and the previous are left in the scene ) https://codesandbox.io/p/sandbox/musing-bush-86gyj7?file=%2Fsrc%2FApp.js%3A36%2C13 Change group key: expect children to be removed from the scene before being re-created or reused ✔ https://codesandbox.io/p/sandbox/floral-wave-5mj74v?file=%2Fsrc%2FApp.js%3A38%2C30

It seems that either some primitive case aren't handled correctly yet or that the documentation should state that no children should be passed to primitives.

AlaricBaraou avatar Mar 11 '24 11:03 AlaricBaraou

The source code is outdated and you can write JSX beneath <primitive />. We've been improving on no.2 specifically and can't get it completely right until v9 which brings architectural changes since this is an inherent issue with React's reconciliation. We're sensitive to the order of operations, and needed a way of separating destructive actions from it -- #2250 being the prime motivation. No. 2 can be sidestepped with a stable key or UUID.

CodyJasonBennett avatar Mar 15 '24 20:03 CodyJasonBennett

cc @krispya I don't see a PR associated with this being closed. What was the resolution for this issue?

Thanks for you work!

gkjohnson avatar May 02 '24 23:05 gkjohnson

cc @CodyJasonBennett

The v9 reconciler updates solve this case. I think all I can do to associate a PR is add some tests.

krispya avatar May 03 '24 01:05 krispya

Gah... found a bug adding the tests. That's what I get for moving too fast.

krispya avatar May 03 '24 01:05 krispya