Skip to content

Visualize Your Ideas With Code

docs(2d): add guidance and warnings on spawners (#280)

DJ3690fa72f135
diff --git a/packages/docs/docs/advanced/spawners.mdx b/packages/docs/docs/advanced/spawners.mdx
index 33b7d66b..1e217dcb 100644
--- a/packages/docs/docs/advanced/spawners.mdx
+++ b/packages/docs/docs/advanced/spawners.mdx
@@ -62,5 +62,23 @@ const pool = range(64).map(i => (
<Circle x={i * 32} width={32} height={32} fill={'lightseagreen'} />
));
-view.add(<Layout layout spawner={() => pool.slice(0, count())} />);
+const layout = createRef<Layout>();
+view.add(<Layout layout ref={layout} spawner={() => pool.slice(0, count())} />);
```
+
+Apart from the spawner function, the pool should never be accessed directly.
+Use the `children()` signal of the parent object to get references to the spawned objects:
+
+```tsx
+// ... continuing from above ...
+let spawnedCircles = layout().children() as Circle[];
+yield* all(...spawnedCircles.map(function* (circle) {
+ yield* circle.scale(1.5, 1).to(1, 1);
+}));
+```
+
+Be aware that the references returned by a call to `children()` may be invalidated
+when the number of spawned objects changes, and accessing the invalidated objects
+may cause undefined behavior. Try not to save references to spawned objects for too long,
+and use `children()` wherever possible to get the updated list of spawned objects.
+