Skip to content
Work in progress. These docs describe Spawnite at launch, and some parts are still being built.

useBehaviour

useBehaviour<T>(behaviour, value): boolean

The one hook a behaviour is built from: adds the trait to the nearest Entity on mount, sets it when the value changes, removes it on unmount. It compares the value field by field with the one it last wrote, so a render with the same props writes nothing and keeps what a system wrote since. When a field changes, it writes the fields the value names and keeps the trait’s other fields. An entity a system destroyed first is left alone. A second behaviour of one trait on an entity is refused, whichever component calls it, since the entity holds one trait of a kind: it throws in development, and in a built game logs once and writes nothing. Returns whether the entity accepted it: true from its first render for the first, and false at every render for a refused second. A component that replaces another by a key on the same entity reads false at its first render and true from its next. A behaviour that does more than set its trait, such as drawing a child or adding a stat, does it only while accepted. The behaviour is a leaf of its entity in the devtools tree, under the calling component’s name with the value as its props.

Type Parameter
T extends Trait
Parameter Type
behaviour Behaviour<T>
value BehaviourValue<T>

boolean