Best Practice: Use imperative code for world mutations
mainWhile <ECS.Entity> is useful for one-off entities (like a Player), you should use imperative code (e.g., ECS.world.add()) for high-frequency or bulk entity management (like enemies or projectiles). Design your React components to react to these changes via <Entities> or useEntities rather than using React to manage the entire lifecycle of every entity.
// 1. Define imperative logic in a module
export const spawnEnemy = () =>
ECS.world.add({
position: { x: 0, y: 0, z: 0 },
velocity: { x: 0, y: 0, z: 0 },
health: 100,
enemy: true
})
// 2. Use React to react to those changes
export const Enemies = () => (
<ECS.Entities in={ECS.world.with("enemy")}>
<ECS.Component name="three">
<EnemyShipModel />
</ECS.Component>
</ECS.Entities>
)