From 4ca8350e01c91eb571dc51495268c3bddff6fa3d Mon Sep 17 00:00:00 2001 From: Johan Santana Date: Thu, 13 Oct 2022 13:19:14 -0400 Subject: [PATCH 1/2] docs: translate useContext.md --- beta/src/content/apis/react/useContext.md | 236 +++++++++++----------- 1 file changed, 118 insertions(+), 118 deletions(-) diff --git a/beta/src/content/apis/react/useContext.md b/beta/src/content/apis/react/useContext.md index bb8bb7e18..6cc49421a 100644 --- a/beta/src/content/apis/react/useContext.md +++ b/beta/src/content/apis/react/useContext.md @@ -4,7 +4,7 @@ title: useContext -`useContext` is a React Hook that lets you read and subscribe to [context](/learn/passing-data-deeply-with-context) from your component. +`useContext` es un Hook de React que te permite leer y suscribirte a un [contexto](/learn/passing-data-deeply-with-context) desde tu componente. ```js const value = useContext(SomeContext) @@ -16,12 +16,12 @@ const value = useContext(SomeContext) --- -## Usage {/*usage*/} +## Uso {/*usage*/} -### Passing data deeply into the tree {/*passing-data-deeply-into-the-tree*/} +### Pasar datos de manera profunda en el árbol {/*passing-data-deeply-into-the-tree*/} -Call `useContext` at the top level of your component to read and subscribe to [context.](/learn/passing-data-deeply-with-context) +Llama `useContext` en el nivel superior de tu componente para leer y suscribirte al [contexto.](/learn/passing-data-deeply-with-context) ```js [[2, 4, "theme"], [1, 4, "ThemeContext"]] import { useContext } from 'react'; @@ -31,9 +31,9 @@ function Button() { // ... ``` -`useContext` returns the context value for the context you passed. To determine the context value, React searches the component tree and finds **the closest context provider above** for that particular context. +`useContext` devuelve el valor del contexto para el contexto que le pasaste. Para determinar el valor del contexto, React busca en el árbol de componentes y encuentra **el proveedor de contexto más cercano arriba** para ese contexto en particular. -To pass context to a `Button`, wrap it or one of its parent components into the corresponding context provider: +Para pasar el contexto a un `Button`, envuélvelo o envuelve a uno de sus componentes padres dentro del proveedor de contexto correspondiente: ```js [[1, 3, "ThemeContext"], [2, 3, "\\"dark\\""], [1, 5, "ThemeContext"]] function MyPage() { @@ -45,15 +45,15 @@ function MyPage() { } function Form() { - // ... renders buttons inside ... + // ... renderiza botones dentro ... } ``` -It doesn't matter how many layers of components there are between the provider and the `Button`. When a `Button` *anywhere* inside of `Form` calls `useContext(ThemeContext)`, it will receive `"dark"` as the value. +No importa cuántas capas de componentes hay entre el proveedor y el `Button`. Cuando un `Button` *en cualquier lugar* dentro de `Form` llama `useContext(ThemeContext)`, recibirá `"dark"` como valor. -`useContext()` always looks for the closest provider *above* the component that calls it. It searches upwards and **does not** consider providers in the component from which you're calling `useContext()`. +`useContext()` siempre busca al proveedor más cercano *arriba* del componente que lo llama. Busca hacia arriba y **no** toma en cuenta a los proveedores en el componente desde el cual estás llamando `useContext()`. @@ -75,8 +75,8 @@ export default function MyApp() { function Form() { return ( - - + + ); } @@ -143,9 +143,9 @@ function Button({ children }) { --- -### Updating data passed via context {/*updating-data-passed-via-context*/} +### Actualizar los datos pasados a través del contexto {/*updating-data-passed-via-context*/} -Often, you'll want the context to change over time. To update context, you need to combine it with [state.](/apis/react/useState) Declare a state variable in the parent component, and pass the current state down as the context value to the provider. +A menudo, querrás que el contexto cambie a través del tiempo. Para actualizar el contexto, necesitas combinarlo con [el estado.](/apis/react/useState) Declara una variable de estado en el componente padre, y pasa el estado actual como el valor de contexto al proveedor. ```js {2} [[1, 4, "ThemeContext"], [2, 4, "theme"], [1, 11, "ThemeContext"]] function MyPage() { @@ -156,20 +156,20 @@ function MyPage() { ); } ``` -Now any `Button` inside of the provider will receive the current `theme` value. If you call `setTheme` to update the `theme` value that you pass to the provider, all `Button` components will re-render with the new `'light'` value. +Ahora cualquier `Button` dentro del proveedor recibirá el valor actual de `theme`. Si llamas `setTheme` para actualizar el valor de `theme` que pasaste al proveedor, todos los componentes `Button` se re-renderizarán con el nuevo valor `'light'`. - + -#### Updating a value via context {/*updating-a-value-via-context*/} +#### Actualizar un valor a través del contexto {/*updating-a-value-via-context*/} -In this example, the `MyApp` component holds a state variable which is then passed to the `ThemeContext` provider. Checking the "Dark mode" checkbox updates the state. Changing the provided value re-renders all the components using that context. +En este ejemplo, el componente `MyApp` guarda una variable de estado la cual es luego pasada al proveedor de `ThemeContext`. Marcar la casilla "Dark mode" actualiza el estado. Cambiar el valor proporcionado re-renderiza todos los componentes que utilizan ese contexto. @@ -191,7 +191,7 @@ export default function MyApp() { setTheme(e.target.checked ? 'dark' : 'light') }} /> - Use dark mode + Usar modo oscuro ) @@ -199,9 +199,9 @@ export default function MyApp() { function Form({ children }) { return ( - - - + + + ); } @@ -267,13 +267,13 @@ function Button({ children }) { -Note that `value="dark"` passes the `"dark"` string, but `value={theme}` passes the value of the JavaScript `theme` variable with [JSX curly braces.](/learn/javascript-in-jsx-with-curly-braces) Curly braces also let you pass context values that aren't strings. +Fíjate que `value="dark"` pasa el string `"dark"`, pero `value={theme}` pasa el valor de la variable JavaScript `theme` con [llaves de JSX.](/learn/javascript-in-jsx-with-curly-braces) Las llaves también te permiten pasar valores de contexto que no son strings. -#### Updating an object via context {/*updating-an-object-via-context*/} +#### Actualizar un objeto a través del contexto {/*updating-an-object-via-context*/} -In this example, there is a `currentUser` state variable which holds an object. You combine `{ currentUser, setCurrentUser }` into a single object and pass it down through the context inside the `value={}`. This lets any component below, such as `LoginButton`, read both `currentUser` and `setCurrentUser`, and then call `setCurrentUser` when needed. +En este ejemplo, hay una variable de estado `currentUser` que guarda un objeto. Puedes combinar `{ currentUser, setCurrentUser }` en un único objeto y luego pasarlo a través del contexto dentro de `value={}`. Esto le permite a cualquier componente debajo, como `LoginButton`, leer tanto `currentUser` como `setCurrentUser`, y luego llamar `setCurrentUser` cuando sea necesario. @@ -298,7 +298,7 @@ export default function MyApp() { function Form({ children }) { return ( - + ); @@ -311,13 +311,13 @@ function LoginButton() { } = useContext(CurrentUserContext); if (currentUser !== null) { - return

You logged in as {currentUser.name}.

; + return

Iniciaste sesión como {currentUser.name}.

; } return ( + }}>Iniciar sesión como Advika ); } @@ -363,9 +363,9 @@ label { -#### Multiple contexts {/*multiple-contexts*/} +#### Múltiples contextos {/*multiple-contexts*/} -In this example, there are two independent contexts. `ThemeContext` provides the current theme, which is a string, while `CurrentUserContext` holds the object representing the current user. +En este ejemplo, hay dos contextos independientes. `ThemeContext` proporciona el tema actual, el cual es un string, mientras que `CurrentUserContext` guarda el objeto que representa el usuario actual. @@ -395,7 +395,7 @@ export default function MyApp() { setTheme(e.target.checked ? 'dark' : 'light') }} /> - Use dark mode + Usar modo oscuro @@ -405,7 +405,7 @@ export default function MyApp() { function WelcomePanel({ children }) { const {currentUser} = useContext(CurrentUserContext); return ( - + {currentUser !== null ? : @@ -417,7 +417,7 @@ function WelcomePanel({ children }) { function Greeting() { const {currentUser} = useContext(CurrentUserContext); return ( -

You logged in as {currentUser.name}.

+

Iniciaste sesión como {currentUser.name}.

) } @@ -429,7 +429,7 @@ function LoginForm() { return ( <> ); @@ -580,7 +580,7 @@ function MyProviders({ children, theme, setTheme }) { function WelcomePanel({ children }) { const {currentUser} = useContext(CurrentUserContext); return ( - + {currentUser !== null ? : @@ -592,7 +592,7 @@ function WelcomePanel({ children }) { function Greeting() { const {currentUser} = useContext(CurrentUserContext); return ( -

You logged in as {currentUser.name}.

+

Iniciaste sesión como {currentUser.name}.

) } @@ -604,7 +604,7 @@ function LoginForm() { return ( <>