UNPKG

nucleux

Version:

Simple, atomic hub for all your React application's state management needs. No providers, no boilerplate, just state that works.

118 lines (117 loc) 4.06 kB
import { ReadOnlyAtomInterface } from './Atom'; import { Store } from './Store'; import { StoreConstructable, StoreProxy } from './types'; /** * Provides access to a Nucleux store instance. * * This hook retrieves a store instance from the container and ensures proper * cleanup when the component unmounts. * * @template S - The store type that extends the base Store class * @param {StoreConstructable<S>} store - The store class constructor * @returns {S} The store instance * * @example * // Define your store * class CounterStore extends Store { * count = this.atom(0); * * increment() { * this.count.value++; * } * } * * // Use the store in a component * function Counter() { * const counterStore = useStore(CounterStore); * const count = useValue(counterStore.count); * * return ( * <div> * <p>Count: {count}</p> * <button onClick={counterStore.increment}>Increment</button> * </div> * ); * } */ declare function useStore<S extends Store>(store: StoreConstructable<S>): S; /** * Subscribes to an atom's value and returns the current value. * * This hook creates a subscription to an atom and triggers re-renders * when the atom's value changes. * * @template V - The type of the atom's value * @param {ReadOnlyAtomInterface<V>} atom - The atom to subscribe to * @returns {V} The current value of the atom * * @example * // Using with an atom directly * const userStore = useStore(UserStore); * const username = useValue(userStore.username); */ declare function useValue<V>(atom: ReadOnlyAtomInterface<V>): V; /** * Directly accesses an atom value from a store by key name. * * This overload allows accessing a specific atom from a store without * explicitly retrieving the store instance first. * * @template S - The store type that extends the base Store class * @template K - The key of the atom in the store * @param {StoreConstructable<S>} store - The store class constructor * @param {K} atomKey - The key of the atom to access * @returns {S[K] extends AtomInterface<infer V> ? V : never} The current value of the atom * * @example * // Direct access to an atom value * const username = useValue(UserStore, "username"); * const isLoggedIn = useValue(UserStore, "isLoggedIn"); */ declare function useValue<S extends Store, K extends keyof S>(store: StoreConstructable<S>, atomKey: K): S[K] extends ReadOnlyAtomInterface<infer V> ? V : never; /** * Provides access to all methods and state values from a Nucleux store in a single hook. * * This hook creates a reactive proxy to the store that automatically updates when any atom * in the store changes. It's designed for components that need access to multiple atoms * from the same store without having to use multiple hooks. * * @template S - The store type that extends the base Store class * @param {StoreConstructable<S>} store - The store class constructor * @returns {StoreProxy<S>} A proxy object containing both store methods and atom values * * @example * // Define your store * class CounterStore extends Store { * count = this.atom(0); * * increment() { * this.count.value++; * } * * decrement() { * this.count.value--; * } * } * * // Use the store in a component * function Counter() { * const counter = useNucleux(CounterStore); * * // Access both methods and state directly * return ( * <div> * <p>Count: {counter.count}</p> * <button onClick={counter.increment}>+</button> * <button onClick={counter.decrement}>-</button> * </div> * ); * } * * @remarks * - This hook subscribes to all atoms in the store, so components will re-render on any state change. * - For more selective reactivity, consider using `useStore` with individual `useValue` hooks instead. * - Cannot directly modify atom values through the proxy; use store methods for state updates. */ declare function useNucleux<S extends Store>(store: StoreConstructable<S>): StoreProxy<S>; export { useNucleux, useStore, useValue };