use-memo-state
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

use-memo-state

A React hook that combines the power of useState and useMemo to create memoized state values.

Installation

npm install use-memo-state
# or
yarn add use-memo-state

Usage

import { useMemoState } from 'use-memo-state';

function MyComponent() {
  const [sum, setSum] = useMemoState((prev = 0) => prev + (newPrice ?? 0), [newPrice]);
  
  return <div>Total: {sum}</div>;
}

API

function useMemoState<T>(
  initializer: (prev: T) => T,
  deps: ReadonlyArray<any>
): [T, (value: T) => void]

Parameters

  • initializer: A function that takes the previous state value and returns the new state value
  • deps: An array of dependencies that will trigger a recalculation when changed

Returns

  • A tuple containing:
    • The memoized state value
    • A function to update the state

License

ISC

Package Sidebar

Install

npm i use-memo-state

Weekly Downloads

4

Version

0.1.0

License

ISC

Unpacked Size

9.53 kB

Total Files

8

Last publish

Collaborators

  • termosa