0.1.2 • Published 4 years ago

@yao-react/use-reduce-value v0.1.2

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

@yao-react/use-reduce-value

React hook to reduce changed values.

Installation

npm install @yao-react/use-reduce-value
yarn add @yao-react/use-reduce-value

Getting started

export const Demo = () => {
  const [index, setIndex] = useState(0);
  const indexes = useReduceValue(
    (acc, a) => [...acc, a], // reducer
    [], // initAcc
    index // value
  );
  return (
    <div>
      <button onClick={() => setIndex(x => x + 1)}>inc index</button>
      <p>Indexes: {String(indexes)}</p>
    </div>
  );
};

API

nametyperequireddescription
reducer(acc, curr) => acctrue
initAccanytrue
valueanytrue
depsany[]false

More words

  • only changed value will trigger reduce to produce new acc
  • if you want to treat continuous same values as different values for this hook, you can wrap the value as [value]