delete
The Statety.delete
function is used to remove a state key and all its subscribers, including those created manually via Statety.subscribe
. This is useful for dynamic or temporary pieces of state that you want to clean up when they are no longer needed. Use with caution, as deleting a key will make it unavailable for future reads or updates.
TypeScript Signature
Section titled “TypeScript Signature”Statety.delete<T>(key: AnyStatetyKey<T>): void
Parameters
Section titled “Parameters”key
– The state key to delete. Can be basic, derived, or computed.
Returns
Section titled “Returns”void
– Does not return a value.
Example
Section titled “Example”Deleting a State Key
Section titled “Deleting a State Key”import { Statety } from "statety";
const USER_KEY = Statety.create<{ username: string }>("user", { username: "guest",});
// Delete the keyStatety.delete(USER_KEY);
// After deletion, reading the key returns nullconsole.log(Statety.read(USER_KEY)); // Output: null
- Deleting a key removes all subscribers associated with it.
- Use this method carefully for dynamic or temporary state to avoid unintended loss of data.
- Any future calls to
Statety.read
orStatety.set
for the deleted key will have no effect.