From 6d82ee20f2088017444b61505bc2dbafead90f0d Mon Sep 17 00:00:00 2001 From: manuel Date: Sat, 2 Aug 2025 22:21:42 +0100 Subject: [PATCH 1/4] feat: translate update-arrays-in-state to pt --- src/content/learn/updating-arrays-in-state.md | 425 ++++++++---------- 1 file changed, 182 insertions(+), 243 deletions(-) diff --git a/src/content/learn/updating-arrays-in-state.md b/src/content/learn/updating-arrays-in-state.md index 61e4f4e2d..d10d9dcd6 100644 --- a/src/content/learn/updating-arrays-in-state.md +++ b/src/content/learn/updating-arrays-in-state.md @@ -1,52 +1,52 @@ --- -title: Updating Arrays in State +title: Atualizando Arrays no State --- -Arrays are mutable in JavaScript, but you should treat them as immutable when you store them in state. Just like with objects, when you want to update an array stored in state, you need to create a new one (or make a copy of an existing one), and then set state to use the new array. +Arrays são mutáveis em JavaScript, mas você deve tratá-los como imutáveis quando os armazena no state. Assim como com objetos, quando você quer atualizar um array armazenado no state, você precisa criar um novo (ou fazer uma cópia de um existente), e então definir o state para usar o novo array. -- How to add, remove, or change items in an array in React state -- How to update an object inside of an array -- How to make array copying less repetitive with Immer +- Como adicionar, remover ou alterar itens em um array no state do React +- Como atualizar um objeto dentro de um array +- Como tornar a cópia de arrays menos repetitiva com Immer -## Updating arrays without mutation {/*updating-arrays-without-mutation*/} +## Atualizando arrays sem mutação {/*updating-arrays-without-mutation*/} -In JavaScript, arrays are just another kind of object. [Like with objects](/learn/updating-objects-in-state), **you should treat arrays in React state as read-only.** This means that you shouldn't reassign items inside an array like `arr[0] = 'bird'`, and you also shouldn't use methods that mutate the array, such as `push()` and `pop()`. +Em JavaScript, arrays são apenas outro tipo de objeto. [Assim como com objetos](/learn/updating-objects-in-state), **você deve tratar arrays no state do React como somente leitura.** Isso significa que você não deve reatribuir itens dentro de um array como `arr[0] = 'passarinho'`, e também não deve usar métodos que mutam o array, como `push()` e `pop()`. -Instead, every time you want to update an array, you'll want to pass a *new* array to your state setting function. To do that, you can create a new array from the original array in your state by calling its non-mutating methods like `filter()` and `map()`. Then you can set your state to the resulting new array. +Em vez disso, toda vez que você quiser atualizar um array, você vai querer passar um *novo* array para sua função de definição do state. Para fazer isso, você pode criar um novo array a partir do array original em seu state chamando seus métodos não-mutantes como `filter()` e `map()`. Então você pode definir seu state para o novo array resultante. -Here is a reference table of common array operations. When dealing with arrays inside React state, you will need to avoid the methods in the left column, and instead prefer the methods in the right column: +Aqui está uma tabela de referência de operações comuns de array. Ao lidar com arrays dentro do state do React, você precisará evitar os métodos na coluna da esquerda, e em vez disso preferir os métodos na coluna da direita: -| | avoid (mutates the array) | prefer (returns a new array) | +| | evitar (muta o array) | preferir (retorna um novo array) | | --------- | ----------------------------------- | ------------------------------------------------------------------- | -| adding | `push`, `unshift` | `concat`, `[...arr]` spread syntax ([example](#adding-to-an-array)) | -| removing | `pop`, `shift`, `splice` | `filter`, `slice` ([example](#removing-from-an-array)) | -| replacing | `splice`, `arr[i] = ...` assignment | `map` ([example](#replacing-items-in-an-array)) | -| sorting | `reverse`, `sort` | copy the array first ([example](#making-other-changes-to-an-array)) | +| adicionar | `push`, `unshift` | `concat`, `[...arr]` sintaxe spread ([exemplo](#adding-to-an-array)) | +| remover | `pop`, `shift`, `splice` | `filter`, `slice` ([exemplo](#removing-from-an-array)) | +| substituir | `splice`, `arr[i] = ...` atribuição | `map` ([exemplo](#replacing-items-in-an-array)) | +| ordenar | `reverse`, `sort` | copie o array primeiro ([exemplo](#making-other-changes-to-an-array)) | -Alternatively, you can [use Immer](#write-concise-update-logic-with-immer) which lets you use methods from both columns. +Alternativamente, você pode [usar Immer](#write-concise-update-logic-with-immer) que permite usar métodos de ambas as colunas. -Unfortunately, [`slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) and [`splice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) are named similarly but are very different: +Infelizmente, [`slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) e [`splice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) têm nomes similares mas são muito diferentes: -* `slice` lets you copy an array or a part of it. -* `splice` **mutates** the array (to insert or delete items). +* `slice` permite copiar um array ou uma parte dele. +* `splice` **muta** o array (para inserir ou deletar itens). -In React, you will be using `slice` (no `p`!) a lot more often because you don't want to mutate objects or arrays in state. [Updating Objects](/learn/updating-objects-in-state) explains what mutation is and why it's not recommended for state. +No React, você estará usando `slice` (sem `p`!) muito mais frequentemente porque você não quer mutar objetos ou arrays no state. [Atualizando Objetos](/learn/updating-objects-in-state) explica o que é mutação e por que não é recomendada para state. -### Adding to an array {/*adding-to-an-array*/} +### Adicionando a um array {/*adding-to-an-array*/} -`push()` will mutate an array, which you don't want: +`push()` vai mutar um array, o que você não quer: @@ -61,7 +61,7 @@ export default function List() { return ( <> -

Inspiring sculptors:

+

Escultores inspiradores:

setName(e.target.value)} @@ -71,7 +71,7 @@ export default function List() { id: nextId++, name: name, }); - }}>Add + }}>Adicionar