subscribe
The Statety.subscribe
function allows you to listen for changes on a specific state key. Whenever the state associated with the key changes, your callback function is invoked. This is useful for non-React code or side effects that need to respond to state updates.
TypeScript Signature
Section titled “TypeScript Signature”Statety.subscribe<T>( key: AnyStatetyKey<T>, callback: (state: T | null) => void): () => void
Parameters
Section titled “Parameters”key
– The state key to subscribe to. Can be basic, derived, or computed.callback
– A function that will be called whenever the state changes.
Returns
Section titled “Returns”() => void
– A function that unsubscribes the listener when called.
Example
Section titled “Example”Subscribing to State Changes
Section titled “Subscribing to State Changes”import { Statety } from "statety";
const USER_KEY = Statety.create<{ username: string; role: string }>("user", { username: "guest", role: "visitor",});
// Subscribe to changesconst unsubscribe = Statety.subscribe(USER_KEY, (state) => { console.log("User state changed!", state);});
// Update stateStatety.set(USER_KEY, { username: "john_doe", role: "admin" });// Console: "User state changed!" { username: "john_doe", role: "admin" }
// Stop listening to changesunsubscribe();
- Subscriptions created using
Statety.subscribe
must be manually removed by calling the returned unsubscribe function to prevent memory leaks if you do not delete the key.