useSeededfunction
Something made once from a seeded stream, and made again only when the seed
changes: useSeeded(seed, (random) => shuffled(random, deck)). The stream
is fresh each time, so the result depends on the seed alone, never on how
many times the component rendered. make is read when the seed changes;
for anything else it depends on, give the component a key.
Import it
import { useSeeded } from "@johnmorrisdotca/tane/react";
Definition
function useSeeded<T>(seed: number, make: (random: Random) => T): T
Parameters
| Name | Type | Description |
|---|---|---|
seed | number | The numeric seed, read as an unsigned 32-bit integer by the stream. |
make | (random: Random) => T | A callback that builds a value from the fresh seeded stream. |
Returns
T
The value built from the seeded stream, retained until the seed changes.
Examples
import { useSeeded } from "@johnmorrisdotca/tane/react";
import { pick } from "@johnmorrisdotca/tane";
import { createElement } from "react";
function Choice() {
const choice = useSeeded(42, (random) => pick(random, ["A", "B", "C"]));
return createElement("p", null, choice);
}
console.log(createElement(Choice));