From b2467a5616a377823def7d2d90fcb8d703e27a37 Mon Sep 17 00:00:00 2001 From: Juan Felipe Rada Date: Tue, 22 Nov 2022 14:06:39 -0300 Subject: [PATCH 01/22] docs: Translate choosing-the-state-structure.md --- .../learn/choosing-the-state-structure.md | 274 +++++++++--------- 1 file changed, 138 insertions(+), 136 deletions(-) diff --git a/beta/src/content/learn/choosing-the-state-structure.md b/beta/src/content/learn/choosing-the-state-structure.md index 3231a70f8..49cbc49da 100644 --- a/beta/src/content/learn/choosing-the-state-structure.md +++ b/beta/src/content/learn/choosing-the-state-structure.md @@ -1,22 +1,22 @@ --- -title: Choosing the State Structure +title: Elección de la estructura del estado --- -Structuring state well can make a difference between a component that is pleasant to modify and debug, and one that is a constant source of bugs. Here are some tips you should consider when structuring state. +Estructurar bien el estado puede marcar la diferencia entre un componente que es agradable de modificar y depurar, y uno que es una fuente constante de errores. Estos son algunos consejos que debe tener en cuenta al estructurar el estado. -* When to use a single vs multiple state variables -* What to avoid when organizing state -* How to fix common issues with the state structure +* Cuando usar una versus multiples variables de estado. +* Qué evitar al organizar el estado. +* Cómo solucionar problemas comunes con la estructura del estado. -## Principles for structuring state {/*principles-for-structuring-state*/} +## Principios para la estructuración del estado {/*principles-for-structuring-state*/} When you write a component that holds some state, you'll have to make choices about how many state variables to use and what the shape of their data should be. While it's possible to write correct programs even with a suboptimal state structure, there are a few principles that can guide you to make better choices: @@ -30,24 +30,24 @@ The goal behind these principles is to *make state easy to update without introd Now let's see how these principles apply in action. -## Group related state {/*group-related-state*/} +## Estado relativo al grupo {/*group-related-state*/} -You might sometimes be unsure between using a single or multiple state variables. +En ocasiones, es posible que no esté seguro entre usar una o varias variables de estado. -Should you do this? +¿Deberías hacer esto? ```js const [x, setX] = useState(0); const [y, setY] = useState(0); ``` -Or this? +¿O esto? ```js const [position, setPosition] = useState({ x: 0, y: 0 }); ``` -Technically, you can use either of these approaches. But **if some two state variables always change together, it might be a good idea to unify them into a single state variable.** Then you won't forget to always keep them in sync, like in this example where moving the cursor updates both coordinates of the red dot: +Técnicamente, puedes usar cualquiera de estos enfoques. Pero **si algunas de las dos variables de estado siempre cambian juntas, podría ser una buena idea unificarlas en una sola variable de estado.** Entonces no olvidará mantenerlos siempre sincronizados, como en este ejemplo donde al mover el cursor se actualizan ambas coordenadas del punto rojo: @@ -93,17 +93,17 @@ body { margin: 0; padding: 0; height: 250px; } -Another case where you'll group data into an object or an array is when you don't know how many different pieces of state you'll need. For example, it's helpful when you have a form where the user can add custom fields. +Otro caso en el que agrupará datos en un objeto o una matriz es cuando no sabe cuántas partes diferentes del estado se necesitarán. Por ejemplo, es útil cuando tienes un formulario en el que el usuario puede agregar campos personalizados. -If your state variable is an object, remember that [you can't update only one field in it](/learn/updating-objects-in-state) without explicitly copying the other fields. For example, you can't do `setPosition({ x: 100 })` in the above example because it would not have the `y` property at all! Instead, if you wanted to set `x` alone, you would either do `setPosition({ ...position, x: 100 })`, or split them into two state variables and do `setX(100)`. +Si tu variable de estado es un objeto, recuerda que [no se puede actualizar solo un campo en él](/learn/updating-objects-in-state) sin copiar explícitamente los otros campos. Por ejemplo, no puedes hacer definir `setPosition({ x: 100 })` pues en el ejemplo anterior no tendría la propiedad `y` en ningún lugar. En su lugar, si quisieras establecer solo la propiedad `x`, la definirías asi `setPosition({ ...position, x: 100 })`, o las dividirías en dos variables de estado y harías `setX(100)`. -## Avoid contradictions in state {/*avoid-contradictions-in-state*/} +## Evitar contradicciones en el estado {/*avoid-contradictions-in-state*/} -Here is a hotel feedback form with `isSending` and `isSent` state variables: +Aquí hay un formulario de comentarios de un hotel con variables de estado `isSending` y `isSent`: @@ -124,12 +124,12 @@ export default function FeedbackForm() { } if (isSent) { - return

Thanks for feedback!

+ return

¡Gracias por tu retroalimentación!

} return (
-

How was your stay at The Prancing Pony?

+

¿Cómo fue tu estadía en The Prancing Pony?