set
The Statety.set function is used to update the value of a state key. You can either replace the value outright or provide an updater function that receives the current state and returns a new value. This allows for fine-grained functional updates.
TypeScript Signature
Section titled “TypeScript Signature”Statety.set<T>( key: StatetyKey<T>, value: T | null | ((state: T | null) => T | null)): voidParameters
Section titled “Parameters”key– The key identifying the basic state to update.value– Either the new value to assign, null, or an updater function that receives the current value and returns the new one.
Returns
Section titled “Returns”void– Does not return a value.
Examples
Section titled “Examples”Replacing the entire State
Section titled “Replacing the entire State”import Statety from "statety";
const USER_KEY = Statety.create<{ username: string; role: string }>("user", { username: "guest", role: "visitor",});
// Replace the whole objectStatety.set(USER_KEY, { username: "john_doe", role: "admin" });Updating part of the State with an updater function
Section titled “Updating part of the State with an updater function”// Update just one fieldStatety.set(USER_KEY, (state) => { if (state) state.role = "superadmin"; return state;});Statety.setworks only with basic state keys.- Derived and computed state are automatically updated based on their dependencies.