-
Notifications
You must be signed in to change notification settings - Fork 0
/
React.js
36 lines (26 loc) · 818 Bytes
/
React.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Use Memo hook
import React, { useMemo, useState } from "react";
const App = () => {
const [count1, setCount1] = useState(0);
const [count2, setCount2] = useState(100);
// How you should be using memo
const squaredValue = useMemo(() => {
console.log("Expensive calculation fired");
return count1 * count1;
}, [count1]);
// How thw component will be rendered without a memo
const squaredValue = () => {
console.log("Expensive calculation fired");
return count1 * count1;
};
return (
<div>
<h1>counter : {count1}</h1>
<h2>Sqaured counter : {squaredValue()}</h2>
<button onClick={() => setCount1(count1 + 1)}></button>
<button onClick={() => setCount2(count2 - 1)}></button>
</div>
);
};
export default App;
// Create a polyfill for useMemo