npm.io
1.0.3 • Published 5 years ago

use-states-react

Licence
ISC
Version
1.0.3
Deps
0
Size
8 kB
Vulns
0
Weekly
0
Stars
3

useStates

A custom hook based on useReducer and inspired by strategy design pattern for having multiple states

With npm:

npm install --save use-states-react

With yarn:

yarn add use-states-react

This is how you are going to use it : =>

import { useStates } from "use-states-react";

const initialState = {
  name: "",
};

const Example = () => {
  const [state, setState] = useStates<any>(initialState);
  const { name } = state;
  const onChange = ({ target: { name, value } }) => {
    setState(
      {
        [name]: value,
      },
      () => console.log("Callback function")
    );
  };
  return (
    <>
      <input name="name" type="text" value={name} onChange={onChange} />
    </>
  );
};

Keywords