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

0.1.3 • Public • Published

use-record

This hook makes it easier to manipulate Records in React

import React from 'react'
import { useRecord } from 'use-record'
 
const initialState: Record<string, number> = { foo: 0, bar: 0, baz: 0 }
const inc = (x: number) => x + 1
const dec = (x: number) => x - 1
 
export const MyComponent: React.FC = () => {
  const [state, setRecord, setRecords] = useRecord(initialState);
 
  return (
    <>
      <ul>
        {Object.entries(state).map(([key, val]) => (
          <li key={key}>
            <button onClick={() => setRecord(key, dec)}>-</button>
            {key}: {val}
            <button onClick={() => setRecord(key, inc)}>+</button>
            <button onClick={() => setRecord(key, 0)}>RESET</button>
          </li>
        ))}
      </ul>
      <button onClick={() => setRecords(initialState)}>RESET ALL</button>
    </>
  );
};

Dependencies (0)

    Dev Dependencies (11)

    Package Sidebar

    Install

    npm i use-record

    Weekly Downloads

    5

    Version

    0.1.3

    License

    MIT

    Unpacked Size

    10.2 kB

    Total Files

    12

    Last publish

    Collaborators

    • josepot