Created
April 25, 2023 21:32
-
-
Save Shalabyelectronics/a7a715051aea97db6550eca517ea9665 to your computer and use it in GitHub Desktop.
Basic usage for React.memo
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 { memo, useState } from 'react'; | |
export default function MyApp() { | |
const [name, setName] = useState(''); | |
const [address, setAddress] = useState(''); | |
return ( | |
<> | |
<label> | |
Name{': '} | |
<input value={name} onChange={e => setName(e.target.value)} /> | |
</label> | |
<label> | |
Address{': '} | |
<input value={address} onChange={e => setAddress(e.target.value)} /> | |
</label> | |
<Greeting name={name} /> | |
</> | |
); | |
} | |
const Greeting = memo(function Greeting({ name }) { | |
console.log("Greeting was rendered at", new Date().toLocaleTimeString()); | |
return <h3>Hello{name && ', '}{name}!</h3>; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment