Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save szaranger/879737abab584a76a85f9b63cdc7c841 to your computer and use it in GitHub Desktop.
Save szaranger/879737abab584a76a85f9b63cdc7c841 to your computer and use it in GitHub Desktop.
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