0.1.0 • Published 5 years ago
@yao-react/use-reduce-callback v0.1.0
@yao-react/use-reduce-callback
React hook to reduce callback invocations.
Installation
npm install @yao-react/use-reduce-callbackyarn add @yao-react/use-reduce-callbackGetting 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:
| name | type | required | description |
|---|---|---|---|
| reduce | (acc, ...args) => acc | true | |
| initAcc | any | true | |
| deps | any[] | false |
Outputs
| name | type | description |
|---|---|---|
| acc | any | |
| handler | (...args) => void |
More words
- The returned
accwill be re-initialized ifdepschanges - The returned
accwill not be re-initialized ifinitAccchanges - The returned
handerwill change if argreducechanges
0.1.0
5 years ago