Skip to content

Instantly share code, notes, and snippets.

@goooseman
Created February 20, 2022 16:30
Show Gist options
  • Save goooseman/cc6ab0b07a5e9a51460b0ecccd2840a1 to your computer and use it in GitHub Desktop.
Save goooseman/cc6ab0b07a5e9a51460b0ecccd2840a1 to your computer and use it in GitHub Desktop.
blog-08-04-key-magic-key-cheatsheet
import Bar from 'components/Bar';
const Foo = ({ inputs }) => inputs.map((input, index /* do NOT use index as a key */) => (
<Bar key={`${input.type}-${input.name}`}>{/* id is the best, if non avail try to generate any unique string, which will be the same for the same item */}
</Bar>
)));
/*
if `inputs` prop was `[{type: 'file', name: 'file'}, { type: 'number', name: 'price' }]`
and then has been changed to `[{type: 'file', name: 'file'}, { type: 'text', name: 'title' }]`
Only the second <Bar> component will be rerendered. First element's key stays the same, so React does not rerender it.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment