useDatafunctionhooknacatamalon
useData(initial: T) => [TDataRecord<T>, TDataSetter<T>]State that lives with a piece of the scene, as [data, setData].
Read it as data.value, change it with the setter, and have something react to it with
useWatch(fn, [data]). Two things far apart can share one: whoever makes it can hand it to
whatever it builds, and each of them watches it without knowing about the others.
Change it through the setter, not with data.value = x. Writing to the field works and is
read back correctly, but nothing is told about it, so a useWatch on it will not run.
It is also what gets saved. A scene written down carries this state and gives it back when the scene is opened again, which is what makes a saved game a saved game and not a fresh start. Keep in it the kind of thing a file can hold: numbers, text, lists and plain objects. A texture or a function is not something a file can keep.
Call it in the scene body, like the rest of the hooks.
Parameters
initialTWhat it holds before anything changes it.
Example
declare const counter: TText;
export const Level: TSceneFn = () => {
const [lives, setLives] = useData(3);
useWatch(() => {
counter.text = `LIVES ${lives.value}`;
}, [lives]);
usePointer().onDown(() => setLives((n) => n - 1));
return createScene();
};