0.1.0 • Published 5 years ago

@yao-react/use-reduce-callback v0.1.0

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

@yao-react/use-reduce-callback

React hook to reduce callback invocations.

Installation

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

Getting started

export const Demo = () => {
  const [
    logs, // returned acc
    handleInputChange, // returned handler
  ] = useReduceCallback(
    // arg reduce
    (acc: string[], e: ChangeEvent<HTMLInputElement>) => [
      ...acc,
      e.target.value,
    ],
    [] // arg initAcc
  );
  return (
    <div>
      <input type="text" onChange={handleInputChange} />
      {logs.map(log => (
        <p>{log}</p>
      ))}
    </div>
  );
};

API

Inputs:

nametyperequireddescription
reduce(acc, ...args) => acctrue
initAccanytrue
depsany[]false

Outputs

nametypedescription
accany
handler(...args) => void

More words

  • The returned acc will be re-initialized if deps changes
  • The returned acc will not be re-initialized if initAcc changes
  • The returned hander will change if arg reduce changes