Last active
October 15, 2022 11:54
-
-
Save SaimumIslam/7c2eeff6ec295594111d7bbb920c0bf0 to your computer and use it in GitHub Desktop.
React re-render solve using react.memo,useMemo
This file contains 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 React, { useState, useMemo } from "react"; | |
const Number = React.memo(({ number }) => { | |
console.log("number render"); | |
return ( | |
<div> | |
<p>Number:{number}</p> | |
</div> | |
); | |
}); | |
const Count = React.memo(({ count }) => { | |
console.log("count render"); | |
return ( | |
<div> | |
<p>Count:{count}</p> | |
</div> | |
); | |
}); | |
const Options = React.memo(({ options }) => { | |
console.log("options render"); | |
return <div>Options:{options}</div>; | |
}); | |
const Home = () => { | |
const [count, setCount] = useState(0); | |
const [number, setNumber] = useState(0); | |
const options = useMemo(() => [1, 2, 3], []); | |
const handleCount = () => { | |
setCount((prev) => prev + 1); | |
}; | |
const handleNumber = () => { | |
setNumber((prev) => prev + 1); | |
}; | |
return ( | |
<div> | |
<Count count={count} /> | |
<button onClick={handleCount}>Numbering</button> | |
<p>-----</p> | |
<Number number={number} /> | |
<button onClick={handleNumber}>Conting</button> | |
<p>-----</p> | |
<Options options={options} /> | |
</div> | |
); | |
}; | |
export default Home; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment