0.0.1 • Published 5 years ago

use-mapped-state v0.0.1

Weekly downloads
9
License
MIT
Repository
github
Last release
5 years ago

A React hook for mapping state on each state change

Usage

import useMappedState from 'use-mapped-state';

const Comp = () => {
  const [state, setState, [value1, yoValue]] = useMappedState(
    'initialValue',
    value => value + 1,
    value => 'Yo' + value
  );

  return (
    <div>
      <span>{state}</span>
      <span>{value1}</span>
      <span>{yoValue}</span>
      <button onClick={() => setState('Cool')} />
    </div>
  );
};

export default Comp;

Typings

The cool thing about this hook is that you get a typed return value of your map functions

const [
  num, // number
  setNum, // Dispatch..
  [
    value1, // number
    value2, // string
    value3, // object
  ],
] = useMappedState(1, value => value + 1, value => 'Yo' + value, value => ({ value }));