Skip to content

Commit b8f81bc

Browse files
committed
📝 update change detection docs
1 parent 26d65b2 commit b8f81bc

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,26 @@ world
364364
.updateEach(([position, velocity]) => {}, { changeDetection: 'always' })
365365
```
366366
367+
Changed detection shallowly compares the scalar values with, just like React. This means objects and arrays will only be detected as changed if a new array or object is committed to the store. While immutable state is a great design pattern, it creates memory pressure and reduces performance so instead you can mutate and manually flag a changed has occured.
368+
369+
```js
370+
// ❌ This change will not be detected since the array is mutated and will pass the comparison
371+
world.query(Inventory).updateEach(([inventory]) => {
372+
inventory.items.push(item)
373+
})
374+
375+
// ✅ This change will be detected since a new array is created and the comparison will fail
376+
world.query(Inventory).updateEach(([inventory]) => {
377+
inventory.items = [...inventory.items, item]
378+
})
379+
380+
// ✅ This change is manually flagged and we still get to mutate for performance
381+
world.query(Inventory).updateEach(([inventory], entity) => {
382+
inventory.items.push(item)
383+
entity.changed()
384+
})
385+
```
386+
367387
### World traits
368388
369389
For global data like time, these can be traits added to the world. **World traits do not appear in queries.**

0 commit comments

Comments
 (0)