UNPKG

xsignals

Version:

A lightweight state management library for React, leveraging the power of Signals for seamless and efficient state handling.

48 lines (47 loc) 3.02 kB
import { State } from "./types/xsignals.types"; /** * The above code defines TypeScript functions for creating and managing state, as well as functions * for using signals and actions with the state. * @param {T} initialState - The `initialState` parameter is the initial value of the state. It can be * of any type and will be used as the starting value for the state. * @returns The `createState` function returns an object with three properties: `getState`, `setState`, * and `add`. */ export declare function createSignal<T>(initialState: T): State<T>; /** * * The `useSignal` function is a TypeScript function that returns a signal value and a setter function * for updating the value in a state object. * @param state - The `state` parameter is an object that represents the current state of your * application. It should have a `getState` method that returns the current state and a `setState` * method that allows you to update the state. * @param key - The `key` parameter is the key of the property in the `state` object that you want to * create a signal for. It is used to access the specific property in the state object and update its * value. * @returns The `useSignal` function returns an array containing two elements. The first element is a * function of type `SignalType<T[keyof T]>`, which represents the current value of the specified key * in the state. The second element is a function that takes an updater function as an argument and * updates the value of the specified key in the state. */ export declare function useSignal<T>(initialState: T): T; export declare const signal: typeof useSignal; /** * The `useActions` function is a TypeScript function that takes in a state and a set of actions, and * returns a record of functions that can be used to dispatch actions to update the state. * @param state - The `state` parameter is an object that represents the current state of your * application. It typically contains various properties that hold different pieces of data. * @param actions - The `actions` parameter is a record object where the keys are action types and the * values are functions that accept a payload and perform some action. * @returns The function `useActions` returns a record of action functions. */ export declare function useStore<T>(prevState: T, actions: Record<string, (payload: T) => void>): Record<string, (payload: T) => void>; /** * The `persistState` function is used to persist the state of an object in local storage, allowing it * to be retrieved and updated across different sessions. * @param state - The `state` parameter is an object that represents the current state of your * application. It can be of any type `T`. * @param {string} key - The `key` parameter is a string that represents the key under which the state * will be stored in the localStorage. It is used to identify the stored state when retrieving or * updating it. */ export declare function persistState<T>(state: State<T>, key: string): void;