use-memoized-callback

0.0.3 • Public • Published

useMemoizedCallback react hook

Install it with yarn:

yarn add use-memoized-callback 

or with npm:

npm i use-memoized-callback  --save

*For more information about callback memoization read this article.

Example

import React, { useState } from 'react';
import useMemoizedCallback  from 'use-memoized-callback '; 
 
 
function App(){
  const [counter, setCounter] = useState(0);
 
  const incCounter = useMemoizedCallback(event => {
    setCounter(counter + 1);
  }); // no dependency required
 
  return (
    <div>
      {/* 
          incCounter is memoized so AnotherComponent will not reRender each time.
      */}
       <AnotherComponent onClick={incCounter} /> 
    </div>
  );
}
 

/use-memoized-callback/

    Package Sidebar

    Install

    npm i use-memoized-callback

    Weekly Downloads

    7

    Version

    0.0.3

    License

    ISC

    Unpacked Size

    3.52 kB

    Total Files

    5

    Last publish

    Collaborators

    • samanm