Created
February 20, 2022 16:30
-
-
Save goooseman/cc6ab0b07a5e9a51460b0ecccd2840a1 to your computer and use it in GitHub Desktop.
blog-08-04-key-magic-key-cheatsheet
This file contains hidden or 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 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