create
The Statety.create
function is used to initialize a new piece of global state and generate a key that can be used to access and update that state throughout your application. This is the fundamental way to define basic reactive state in Statety.
TypeScript Signature
Section titled “TypeScript Signature”Statety.create<T>(keyName: string, defaultValue?: T | null): StatetyKey<T>
Parameters
Section titled “Parameters”T
– The type of the state value.keyName
– A descriptive name for the key (used mainly for debugging).defaultValue
– Optional initial value for the state. Defaults tonull
if not provided.
Returns
Section titled “Returns”StatetyKey<T>
– A unique key that identifies the created state.
Examples
Section titled “Examples”Basic Usage
Section titled “Basic Usage”import { Statety } from "statety";
// Create a basic global state keyconst USER_KEY = Statety.create<{ username: string; role: string }>("user", { username: "guest", role: "visitor",});
Without Initial Value
Section titled “Without Initial Value”import { Statety } from "statety";
// State without providing a default valueconst COUNTER_KEY = Statety.create<number>("counter");