Skip to content

Instantly share code, notes, and snippets.

@Shalabyelectronics
Created April 25, 2023 21:32
Show Gist options
  • Save Shalabyelectronics/a7a715051aea97db6550eca517ea9665 to your computer and use it in GitHub Desktop.
Save Shalabyelectronics/a7a715051aea97db6550eca517ea9665 to your computer and use it in GitHub Desktop.
Basic usage for React.memo
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