tsuite
Version:
A collection of useful utility functions, All fully typed and documented
21 lines • 871 B
TypeScript
//#region src/create-state.d.ts
type StateSideEffect<T> = (currentValue: T, previousValue?: T) => void;
/**
* Creates a stateful value with an associated side-effect that runs
* whenever the state is updated.
*
* @template T The type of the state value.
* @param initialValue The initial value of the state.
* @param callbackfn A function to run with the new and (optionally) previous value after each update.
* @returns A tuple containing a getter and a setter for the state.
*
* @example
* const [getCount, setCount] = createState(0, (value, prev) => {
* console.log('Count changed from', prev, 'to', value);
* });
* setCount(1); // Logs: Count changed from 0 to 1
* console.log(getCount()); // 1
*/
declare function createState<T>(initialValue: T, callbackfn: StateSideEffect<T>): [() => T, (newValue: T) => void];
//#endregion
export { createState };