You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/docs/hooks-overview.md
+60-60Lines changed: 60 additions & 60 deletions
Original file line number
Diff line number
Diff line change
@@ -1,30 +1,30 @@
1
1
---
2
2
id: hooks-overview
3
-
title: Hooks at a Glance
3
+
title: Hooks a Simple Vista
4
4
permalink: docs/hooks-overview.html
5
5
next: hooks-state.html
6
6
prev: hooks-intro.html
7
7
---
8
8
9
-
*Hooks*are a new addition in React 16.8. They let you use state and other React features without writing a class.
9
+
Los *Hooks*son una nueva incorporación en React 16.8. Te permiten usar estado y otras características de React sin escribir una clase.
10
10
11
-
Hooks are [backwards-compatible](/docs/hooks-intro.html#no-breaking-changes). This page provides an overview of Hooks for experienced React users. This is a fast-paced overview. If you get confused, look for a yellow box like this:
11
+
Hooks son [compatibles con versiones anteriores](/docs/hooks-intro.html#no-breaking-changes). Esta página proporciona una descripción general de Hooks para usuarios experimentados de React. Esta es una rápida mirada. Si te confundes, busca un recuadro amarillo como este:
12
12
13
-
>Detailed Explanation
13
+
>Explicación Detallada
14
14
>
15
-
>Read the [Motivation](/docs/hooks-intro.html#motivation)to learn why we're introducing Hooks to React.
15
+
>Lee la [Motivacion](/docs/hooks-intro.html#motivation)para entender por qué estamos introduciendo Hooks a React.
16
16
17
-
**↑↑↑ Each section ends with a yellow box like this.**They link to detailed explanations.
17
+
**↑↑↑ Cada sección termina con un recuadro amarillo como este.**Ellos vinculan a explicaciones detalladas.
18
18
19
-
## 📌 State Hook {#-state-hook}
19
+
## 📌 Hook de Estado {#-state-hook}
20
20
21
-
This example renders a counter. When you click the button, it increments the value:
21
+
Este ejemplo renderiza un contador. Cuando haces click en el botón, incrementa el valor:
22
22
23
23
```js{1,4,5}
24
24
import React, { useState } from 'react';
25
25
26
26
function Example() {
27
-
// Declare a new state variable, which we'll call "count"
27
+
// Declara una nueva variable de estado, que llamaremos "count".
28
28
const [count, setCount] = useState(0);
29
29
30
30
return (
@@ -38,53 +38,53 @@ function Example() {
38
38
}
39
39
```
40
40
41
-
Here, `useState`is a*Hook* (we'll talk about what this means in a moment). We call it inside a function component to add some local state to it. React will preserve this state between re-renders. `useState`returns a pair: the *current* state value and a function that lets you update it. You can call this function from an event handler or somewhere else. It's similar to`this.setState`in a class, except it doesn't merge the old and new state together. (We'll show an example comparing `useState`to `this.state`in [Using the State Hook](/docs/hooks-state.html).)
41
+
Aquí, `useState`es un*Hook* (hablaremos de lo que esto significa en un momento). Lo llamamos dentro de un componente funcional para agregarle un estado local. React mantendrá este estado entre re-renderizados. `useState`devuelve un par: el valor de estado *actual* y una función que le permite actualizarlo. Puedes llamar a esta función desde un controlador de eventos o desde otro lugar. Es similar a`this.setState`en una clase, excepto que no combina el estado antiguo y el nuevo. (Mostraremos un ejemplo comparando `useState`con`this.state`en [Usando el Hook de Estado](/docs/hooks-state.html).)
42
42
43
-
The only argument to`useState`is the initial state. In the example above, it is `0`because our counter starts from zero. Note that unlike `this.state`, the state here doesn't have to be an object -- although it can be if you want. The initial state argument is only used during the first render.
43
+
El único argumento para`useState`es el estado inicial. En el ejemplo anterior, es `0`porque nuestro contador comienza desde cero. Ten en cuenta que a diferencia de `this.state`, el estado aquí no tiene que ser un objeto -- aunque puede serlo si quisieras. El argumento de estado inicial solo se usa durante el primer renderizado.
44
44
45
-
#### Declaring multiple state variables {#declaring-multiple-state-variables}
45
+
#### Declarando múltiples variables de estado {#declaring-multiple-state-variables}
46
46
47
-
You can use the State Hook more than once in a single component:
47
+
Puedes usar el Hook de Estado más de una vez en un mismo componente:
The [array destructuring](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Array_destructuring) syntax lets us give different names to the state variables we declared by calling`useState`. These names aren't a part of the `useState` API. Instead, React assumes that if you call`useState`many times, you do it in the same order during every render. We'll come back to why this works and when this is useful later.
59
+
La sintáxis de [desestructuración de un array](https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Operadores/Destructuring_assignment) nos permite dar diferentes nombres a las variables de estado que declaramos llamando a`useState`. Estos nombres no forman parte de la API `useState`. En su lugar, React asume que si llamas a`useState`muchas veces, lo haces en el mismo orden durante cada renderizado. Volveremos a explicar por qué esto funciona y cuándo será útil más adelante.
60
60
61
-
#### But what is a Hook? {#but-what-is-a-hook}
61
+
#### Pero qué es un Hook? {#but-what-is-a-hook}
62
62
63
-
Hooks are functions that let you “hook into” React state and lifecycle features from function components. Hooks don't work inside classes -- they let you use React without classes. (We [don't recommend](/docs/hooks-intro.html#gradual-adoption-strategy)rewriting your existing components overnight but you can start using Hooks in the new ones if you'd like.)
63
+
Hooks son funciones que te permiten "enganchar" el estado de React y el ciclo de vida desde componentes funcionales. Los hooks no funcionan dentro de las clases -- te permiten usar React sin clases. ([No recomendamos](/docs/hooks-intro.html#gradual-adoption-strategy)reescribir tus componentes existentes de la noche a la mañana, pero puedes comenzar a usar Hooks en los nuevos si quieres.)
64
64
65
-
React provides a few built-in Hooks like `useState`. You can also create your own Hooks to reuse stateful behavior between different components. We'll look at the built-in Hooks first.
65
+
React proporciona algunos Hooks incorporados como `useState`. También puedes crear tus propios Hooks para reutilizar el comportamiento con estado entre diferentes componentes. Primero veremos los Hooks incorporados.
66
66
67
-
>Detailed Explanation
67
+
>Explicación Detallada
68
68
>
69
-
>You can learn more about the State Hook on a dedicated page: [Using the State Hook](/docs/hooks-state.html).
69
+
>Puedes aprender más sobre el Hook de Estado en la pagina dedicada: [Usando el Hook de Estado](/docs/hooks-state.html).
70
70
71
-
## ⚡️ Effect Hook {#️-effect-hook}
71
+
## ⚡️ Hook de Efecto {#️-effect-hook}
72
72
73
-
You've likely performed data fetching, subscriptions, or manually changing the DOM from React components before. We call these operations "side effects" (or "effects" for short) because they can affect other components and can't be done during rendering.
73
+
Es probable que hayas realizado recuperación de datos, suscripciones o modificacion manual del DOM desde los componentes de React. Llamamos a estas operaciones "efectos secundarios" (o "efectos" para abreviar) porque pueden afectar a otros componentes y no se pueden hacer durante el renderizado.
74
74
75
-
The Effect Hook, `useEffect`, adds the ability to perform side effects from a function component. It serves the same purpose as `componentDidMount`,`componentDidUpdate`, and`componentWillUnmount`in React classes, but unified into a single API. (We'll show examples comparing `useEffect`to these methods in [Using the Effect Hook](/docs/hooks-effect.html).)
75
+
El Hook de Efecto, `useEffect`, agrega la capacidad de realizar efectos secundarios desde un componente funcional. Tiene el mismo propósito que `componentDidMount`,`componentDidUpdate` y`componentWillUnmount`en las clases React, pero unificadas en una sola API. (Mostraremos ejemplos comparando `useEffect`con estos métodos en [Usando el Hook de Efecto](/docs/hooks-effect.html).)
76
76
77
-
For example, this component sets the document title after React updates the DOM:
77
+
Por ejemplo, este componente establece el título del documento después de que React actualiza el DOM:
78
78
79
79
```js{1,6-10}
80
80
import React, { useState, useEffect } from 'react';
81
81
82
82
function Example() {
83
83
const [count, setCount] = useState(0);
84
84
85
-
// Similar to componentDidMount and componentDidUpdate:
85
+
// Similar a componentDidMount y componentDidUpdate:
86
86
useEffect(() => {
87
-
// Update the document title using the browser API
87
+
// Actualiza el título del documento usando la Browser API
88
88
document.title = `You clicked ${count} times`;
89
89
});
90
90
@@ -99,9 +99,9 @@ function Example() {
99
99
}
100
100
```
101
101
102
-
When you call`useEffect`, you're telling React to run your "effect" function after flushing changes to the DOM. Effects are declared inside the component so they have access to its props and state. By default, React runs the effects after every render -- *including* the first render. (We'll talk more about how this compares to class lifecycles in [Using the Effect Hook](/docs/hooks-effect.html).)
102
+
Cuando llamas a`useEffect`, le estás diciendo a React que ejecute tu función de "efecto" después de vaciar los cambios en el DOM. Los efectos se declaran dentro del componente para que tengan acceso a sus props y estado. De forma predeterminada, React ejecuta los efectos después de cada renderizado -- *incluyendo* el primer renderizado. (Hablaremos más sobre cómo se compara esto con los ciclos de vida de una clase en [Usando el Hook de Efecto](/docs/hooks-effect.html).)
103
103
104
-
Effects may also optionally specify how to "clean up" after them by returning a function. For example, this component uses an effect to subscribe to a friend's online status, and cleans up by unsubscribing from it:
104
+
Los efectos también pueden especificar opcionalmente cómo "limpiar" después de ellos devolviendo una función. Por ejemplo, este componente utiliza un efecto para suscribirse al estado en línea de un amigo, y se limpia al anular su suscripción:
105
105
106
106
```js{10-16}
107
107
import React, { useState, useEffect } from 'react';
@@ -128,9 +128,9 @@ function FriendStatus(props) {
128
128
}
129
129
```
130
130
131
-
In this example, React would unsubscribe from our `ChatAPI`when the component unmounts, as well as before re-running the effect due to a subsequent render. (If you want, there's a way to [tell React to skip re-subscribing](/docs/hooks-effect.html#tip-optimizing-performance-by-skipping-effects)if the`props.friend.id`we passed to `ChatAPI`didn’t change.)
131
+
En este ejemplo, React cancelará la suscripción de nuestra `ChatAPI`cuando se desmonte el componente, así como antes de volver a ejecutar el efecto debido a un renderizado posterior. (Si prefieres, hay una manera de [decirle a React que omita la re-suscripcion](/docs/hooks-effect.html#tip-optimizing-performance-by-skipping-effects)si el`props.friend.id`que pasamos a la `ChatAPI`no ha cambiado.)
132
132
133
-
Just like with `useState`, you can use more than a single effect in a component:
133
+
Al igual que con `useState`, puedes usar más de un solo efecto en un componente:
134
134
135
135
```js{3,8}
136
136
function FriendStatusWithCounter(props) {
@@ -153,32 +153,32 @@ function FriendStatusWithCounter(props) {
153
153
// ...
154
154
```
155
155
156
-
Hooks let you organize side effects in a component by what pieces are related (such as adding and removing a subscription), rather than forcing a split based on lifecycle methods.
156
+
Los Hooks te permiten organizar efectos secundarios en un componente según qué partes están relacionadas (como agregar y eliminar una suscripción), en lugar de forzar una división basada en métodos del ciclo de vida.
157
157
158
-
>Detailed Explanation
158
+
>Explicación detallada
159
159
>
160
-
>You can learn more about `useEffect`on a dedicated page: [Using the Effect Hook](/docs/hooks-effect.html).
160
+
>Puede obtener más información sobre `useEffect`en la página dedicada: [Usando el Hook de Efecto](/docs/hooks-effect.html).
161
161
162
-
## ✌️ Rules of Hooks {#️-rules-of-hooks}
162
+
## ✌️ Reglas de Hooks {#️-rules-of-hooks}
163
163
164
-
Hooks are JavaScript functions, but they impose two additional rules:
164
+
Hooks son funciones de JavaScript, pero imponen dos reglas adicionales:
165
165
166
-
*Only call Hooks **at the top level**. Don’t call Hooks inside loops, conditions, or nested functions.
167
-
*Only call Hooks **from React function components**. Don’t call Hooks from regular JavaScript functions. (There is just one other valid place to call Hooks -- your own custom Hooks. We'll learn about them in a moment.)
166
+
*Solo llamar Hooks **en el nivel superior**. No llames Hooks dentro de loops, condiciones o funciones anidadas.
167
+
*Solo llamar Hooks **desde componentes funcionales de React**. No llames Hooks desde las funciones regulares de JavaScript. (Solo hay otro lugar válido para llamar Hooks: tus propios Hooks personalizados. En un momento aprenderemos sobre estos).
168
168
169
-
We provide a [linter plugin](https://www.npmjs.com/package/eslint-plugin-react-hooks)to enforce these rules automatically. We understand these rules might seem limiting or confusing at first, but they are essential to making Hooks work well.
169
+
Proporcionamos un [plugin de linter](https://www.npmjs.com/package/eslint-plugin-react-hooks)para forzar estas reglas automáticamente. Entendemos que estas reglas pueden parecer limitantes o confusas al principio, pero son esenciales para hacer que los Hooks funcionen bien.
170
170
171
-
>Detailed Explanation
171
+
>Explicación Detallada
172
172
>
173
-
>You can learn more about these rules on a dedicated page: [Rules of Hooks](/docs/hooks-rules.html).
173
+
>Puedes aprender más sobre estas reglas en la página dedicada: [Reglas de Hooks](/docs/hooks-rules.html).
174
174
175
-
## 💡 Building Your Own Hooks {#-building-your-own-hooks}
175
+
## 💡 Construyendo Tus Propios Hooks {#-building-your-own-hooks}
176
176
177
-
Sometimes, we want to reuse some stateful logic between components. Traditionally, there were two popular solutions to this problem: [higher-order components](/docs/higher-order-components.html)and[render props](/docs/render-props.html). Custom Hooks let you do this, but without adding more components to your tree.
177
+
A veces, queremos reutilizar alguna lógica de estado entre componentes. Tradicionalmente, había dos soluciones populares para este problema: [componente de orden superior](/docs/higher-order-components.html)y[render props](/docs/render-props.html). Los Hooks personalizados te permiten hacer esto, pero sin agregar más componentes a tu árbol.
178
178
179
-
Earlier on this page, we introduced a`FriendStatus`component that calls the `useState`and`useEffect`Hooks to subscribe to a friend's online status. Let's say we also want to reuse this subscription logic in another component.
179
+
Anteriormente en esta página, presentamos un componente`FriendStatus`que llama a los Hooks`useState`y`useEffect`para suscribirse al estado en línea de un amigo. Digamos que también queremos reutilizar esta lógica de suscripción en otro componente.
180
180
181
-
First, we'll extract this logic into a custom Hook called`useFriendStatus`:
181
+
Primero, extraeremos esta lógica en un Hook personalizado llamado`useFriendStatus`:
182
182
183
183
```js{3}
184
184
import React, { useState, useEffect } from 'react';
@@ -201,9 +201,9 @@ function useFriendStatus(friendID) {
201
201
}
202
202
```
203
203
204
-
It takes `friendID`as an argument, and returns whether our friend is online.
204
+
Toma `friendID`como argumento, y retorna si nuestro amigo está en línea o no.
205
205
206
-
Now we can use it from both components:
206
+
Ahora lo podemos usar desde ambos componentes:
207
207
208
208
209
209
```js{2}
@@ -229,19 +229,19 @@ function FriendListItem(props) {
229
229
}
230
230
```
231
231
232
-
The state of these components is completely independent. Hooks are a way to reuse *stateful logic*, not state itself. In fact, each *call* to a Hook has a completely isolated state -- so you can even use the same custom Hook twice in one component.
232
+
El estado de estos componentes es completamente independiente. Los Hooks son una forma de reutilizar *la lógica de estado*, no el estado en sí. De hecho, cada *llamada* a un Hook tiene un estado completamente aislado -- por lo que incluso puedes usar el mismo Hook personalizado dos veces en un componente.
233
233
234
-
Custom Hooks are more of a convention than a feature. If a function's name starts with "`use`" and it calls other Hooks, we say it is a custom Hook. The `useSomething`naming convention is how our linter plugin is able to find bugs in the code using Hooks.
234
+
Los Hooks personalizados son más una convención que una funcionalidad. Si el nombre de una función comienza con "`use`" y llama a otros Hooks, decimos que es un Hook personalizado. La convención de nomenclatura `useSomething`es cómo nuestro plugin de linter puede encontrar errores en el código usando Hooks.
235
235
236
-
You can write custom Hooks that cover a wide range of use cases like form handling, animation, declarative subscriptions, timers, and probably many more we haven't considered. We are excited to see what custom Hooks the React community will come up with.
236
+
Puedes escribir Hooks personalizados que cubran una amplia gama de casos de uso como manejo de formularios, animación, suscripciones declarativas, temporizadores y probablemente muchos más que no hemos considerado. Estamos muy entusiasmados de ver los Hooks personalizados que la comunidad de React creará.
237
237
238
-
>Detailed Explanation
238
+
>Explicación Detallada
239
239
>
240
-
>You can learn more about custom Hooks on a dedicated page: [Building Your Own Hooks](/docs/hooks-custom.html).
240
+
>>Puedes aprender más sobre Hooks personalizados en la página dedicada: [Construyendo Tus Propios Hooks](/docs/hooks-custom.html).
241
241
242
-
## 🔌 Other Hooks {#-other-hooks}
242
+
## 🔌 Otros Hooks {#-other-hooks}
243
243
244
-
There are a few less commonly used built-in Hooks that you might find useful. For example, [`useContext`](/docs/hooks-reference.html#usecontext)lets you subscribe to React context without introducing nesting:
244
+
Hay algunos Hooks incorporados de uso menos común que pueden resultarte útiles. Por ejemplo, [`useContext`](/docs/hooks-reference.html#usecontext)te permite suscribirte al contexto React sin introducir el anidamiento:
245
245
246
246
```js{2,3}
247
247
function Example() {
@@ -251,22 +251,22 @@ function Example() {
251
251
}
252
252
```
253
253
254
-
And[`useReducer`](/docs/hooks-reference.html#usereducer)lets you manage local state of complex components with a reducer:
254
+
Y[`useReducer`](/docs/hooks-reference.html#usereducer)te permite manejar el estado local de componentes complejos con un reducer:
>You can learn more about all the built-in Hooks on a dedicated page: [Hooks API Reference](/docs/hooks-reference.html).
264
+
>>Puedes aprender más sobre todos los Hooks incorporados en la página dedicada: [Referencia de la Hooks API](/docs/hooks-reference.html).
265
265
266
-
## Next Steps {#next-steps}
266
+
## Próximos Pasos {#next-steps}
267
267
268
-
Phew, that was fast! If some things didn't quite make sense or you'd like to learn more in detail, you can read the next pages, starting with the [State Hook](/docs/hooks-state.html) documentation.
268
+
¡Uf, eso fue rápido! Si algunas cosas no te hacen mucho sentido o si te gustaria aprender más en detalle, puedes leer las siguientes páginas, comenzando con la documentación de [Hook de Estado](/docs/hooks-state.html).
269
269
270
-
You can also check out the [Hooks API reference](/docs/hooks-reference.html)and the [Hooks FAQ](/docs/hooks-faq.html).
270
+
También puede consultar la [Referencia de la Hooks API](/docs/hooks-reference.html)y las [Preguntas Frecuentes sobre Hooks](/docs/hooks-faq.html).
271
271
272
-
Finally, don't miss the [introduction page](/docs/hooks-intro.html)which explains *why* we're adding Hooks and how we'll start using them side by side with classes -- without rewriting our apps.
272
+
Finalmente, no dejes de visitar la [página de introducción](/docs/hooks-intro.html)que explica *por qué* estamos agregando Hooks y cómo comenzaremos a usarlos juntos a las clases -- sin volver a escribir nuestras aplicaciones.
0 commit comments