Created
July 12, 2022 11:53
-
-
Save szaranger/879737abab584a76a85f9b63cdc7c841 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 { useMemo, useState } from "react"; | |
| function Child({ total }) { | |
| return <p>{`Total: ${total}`}</p>; | |
| } | |
| export default function Parent() { | |
| const [a, setA] = useState(0); | |
| const [b, setB] = useState(0); | |
| function calc(x, y) { | |
| return x + y; | |
| } | |
| const memoizedValue = useMemo(() => { | |
| 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 total={memoizedValue} /> | |
| </div> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment