#thedivisor#Notice that all the sandboxes above show an error in the console:#thedivisor#You need to give each array item a <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">key</code> — a string or a number that uniquely identifies it among other items in that array:#thedivisor#JSX elements directly inside a <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">map()</code> call always need keys!#thedivisor#Keys tell React which array item each component corresponds to, so that it can match them up later. This becomes important if your array items can move (e.g. due to sorting), get inserted, or get deleted. A well-chosen <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px">key</code> helps React infer what exactly has happened, and make the correct updates to the DOM tree.#thedivisor#Rather than generating keys on the fly, you should include them in your data:#thedivisor#What do you do when each item needs to render not one, but several DOM nodes?#thedivisor#The short <a class="inline text-link dark:text-link-dark border-b border-link border-opacity-0 hover:border-opacity-100 duration-100 ease-in transition leading-normal" href="/reference/react/Fragment"><code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><>...</></code> Fragment</a> syntax won’t let you pass a key, so you need to either group them into a single <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><div></code>, or use the slightly longer and <a class="inline text-link dark:text-link-dark border-b border-link border-opacity-0 hover:border-opacity-100 duration-100 ease-in transition leading-normal" href="/reference/react/Fragment#rendering-a-list-of-fragments">more explicit <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><Fragment></code> syntax:</a>#thedivisor#Fragments disappear from the DOM, so this will produce a flat list of <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><h1></code>, <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><p></code>, <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><h1></code>, <code dir="ltr" class="inline text-code text-secondary dark:text-secondary-dark px-1 rounded-md no-underline bg-gray-30 bg-opacity-10 py-px"><p></code>, and so on.#thedivisor#Different sources of data provide different sources of keys:#thedivisor#<li class="leading-relaxed mb-1"><strong class="font-bold">Data from a database:</strong> If your data is coming from a database, you can use the database keys/IDs, which are unique by nature.</li>
#thedivisor#Observe que todos os sandboxes acima mostram um erro no console:#thedivisor#Você precisa dar a cada item do array uma key — uma string ou um número que identifique de forma única esse item entre outros itens nesse array:#thedivisor#Elementos JSX diretamente dentro de uma chamada map() sempre precisam de keys!#thedivisor#As keys informam ao React a que item do array cada componente corresponde, para que ele possa combiná-los mais tarde. Isso se torna importante se seus itens do array podem se mover (por exemplo, devido a ordenação), serem inseridos ou serem deletados. Uma key bem escolhida ajuda o React a inferir o que exatamente aconteceu e fazer as atualizações corretas na árvore DOM.#thedivisor#Em vez de gerar keys dinamicamente, você deve incluí-las nos seus dados:#thedivisor#O que você faz quando cada item precisa renderizar não um, mas vários nós DOM?#thedivisor#A sintaxe curta de <>...</> Fragment não permite que você passe uma key, então você precisa agrupar os itens em um único <div>, ou usar a sintaxe de <Fragment> que é levemente mais longa e mais explícita:#thedivisor#Fragmentos não aparecem no DOM, então isso irá produzir uma lista plana de <h1>, <p>, <h1>, <p>, e assim por diante.#thedivisor#Diferentes fontes de dados fornecem diferentes fontes de keys:#thedivisor#Dados de um banco de dados: Se seus dados estão vindo de um banco de dados, você pode usar as chaves/IDs do banco de dados, que são únicas por natureza.