Created
          July 12, 2022 11:51 
        
      - 
      
 - 
        
Save szaranger/38b64fe9b6790cde22ebb067182de978 to your computer and use it in GitHub Desktop.  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import { useCallback, useState } from "react"; | |
| function Child({ calc }) { | |
| return <p>{`Total: ${calc()}`}</p>; | |
| } | |
| export default function Parent() { | |
| const [a, setA] = useState(0); | |
| const [b, setB] = useState(0); | |
| function calc(x, y) { | |
| return x + y; | |
| } | |
| const memoizedCalc = useCallback(() => { | |
| return calc(a, b); | |
| }, [a, b]); | |
| return ( | |
| <div className="App"> | |
| <h1>Calculate using useCallback hook</h1> | |
| <> | |
| <p> | |
| <button onClick={() => setA(a + 1)}>a + 1</button> = {a} | |
| </p> | |
| <p> | |
| <button onClick={() => setB(b + 1)}>b + 1</button> = {b} | |
| </p> | |
| </> | |
| <Child calc={memoizedCalc} /> | |
| </div> | |
| ); | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment