Skip to content

Commit 8ebf5de

Browse files
Merge pull request #113 from jnosornov/typechecking-with-proptypes-translation
Translate: Typechecking With PropTypes
2 parents 95ee4a5 + 80f781a commit 8ebf5de

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed
Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
---
22
id: typechecking-with-proptypes
3-
title: Typechecking With PropTypes
3+
title: Verificación de tipos con PropTypes
44
permalink: docs/typechecking-with-proptypes.html
55
redirect_from:
66
- "docs/react-api.html#typechecking-with-proptypes"
77
---
88

9-
> Note:
9+
> Nota:
1010
>
11-
> `React.PropTypes` has moved into a different package since React v15.5. Please use [the `prop-types` library instead](https://www.npmjs.com/package/prop-types).
11+
> `React.PropTypes` se movió a un paquete diferente desde React v15.5. Por favor usa [en su lugar la biblioteca `prop-types`](https://www.npmjs.com/package/prop-types).
1212
>
13-
>We provide [a codemod script](/blog/2017/04/07/react-v15.5.0.html#migrating-from-reactproptypes) to automate the conversion.
13+
>Nosotros ofrecemos [un script de codemod](/blog/2017/04/07/react-v15.5.0.html#migrating-from-reactproptypes) para automatizar la conversión.
1414
15-
As your app grows, you can catch a lot of bugs with typechecking. For some applications, you can use JavaScript extensions like [Flow](https://flow.org/) or [TypeScript](https://www.typescriptlang.org/) to typecheck your whole application. But even if you don't use those, React has some built-in typechecking abilities. To run typechecking on the props for a component, you can assign the special `propTypes` property:
15+
A medida que tu aplicación crece, puedes capturar una gran cantidad de errores con verificación de tipos. Para algunas aplicaciones, puedes usar extensiones de Javascript como [Flow](https://flow.org/) o [TypeScript](https://www.typescriptlang.org/) para verificar los tipos en tu aplicación. Pero incluso si no usas alguno de ellos, React tiene algunas habilidades de verificación de tipos incorporadas. Para usar verificación de tipos en las props de un component, puedes asignar la propiedad especial `PropTypes`:
1616

1717
```javascript
1818
import PropTypes from 'prop-types';
@@ -30,18 +30,18 @@ Greeting.propTypes = {
3030
};
3131
```
3232

33-
`PropTypes` exports a range of validators that can be used to make sure the data you receive is valid. In this example, we're using `PropTypes.string`. When an invalid value is provided for a prop, a warning will be shown in the JavaScript console. For performance reasons, `propTypes` is only checked in development mode.
33+
`PropTypes` exporta un rango de validadores que pueden ser usados para estar seguros que la información recibida sea válida. En este ejemplo, usamos `PropTypes.string`. Cuando un valor inválido se asigna a una prop, se muestra una advertencia en la consola de Javascript. Por razones de desempeño, `PropTypes` solo se verifica en modo desarrollo.
3434

3535
### PropTypes
3636

37-
Here is an example documenting the different validators provided:
37+
Aquí hay un ejemplo que documenta los diferentes tipos de validadores:
3838

3939
```javascript
4040
import PropTypes from 'prop-types';
4141

4242
MyComponent.propTypes = {
43-
// You can declare that a prop is a specific JS type. By default, these
44-
// are all optional.
43+
// Puedes declarar que una propiedad es un tipo específico de JS. Por defecto, estas
44+
// son todas opcionales.
4545
optionalArray: PropTypes.array,
4646
optionalBool: PropTypes.bool,
4747
optionalFunc: PropTypes.func,
@@ -50,50 +50,50 @@ MyComponent.propTypes = {
5050
optionalString: PropTypes.string,
5151
optionalSymbol: PropTypes.symbol,
5252

53-
// Anything that can be rendered: numbers, strings, elements or an array
54-
// (or fragment) containing these types.
53+
// Cualquier cosa que sea interpretada: números, cadenas, elementos o un array
54+
// (o fragment) que contengan estos tipos.
5555
optionalNode: PropTypes.node,
5656

57-
// A React element.
57+
// Un elemento de React
5858
optionalElement: PropTypes.element,
5959

60-
// You can also declare that a prop is an instance of a class. This uses
61-
// JS's instanceof operator.
60+
// Además puedes declarar que una prop es una instancia de una clase. Este usa
61+
// el operador instanceof de Js.
6262
optionalMessage: PropTypes.instanceOf(Message),
6363

64-
// You can ensure that your prop is limited to specific values by treating
65-
// it as an enum.
64+
// Puedes asegurar que una prop esta limitada a valores específicos si se
65+
// considera como enum.
6666
optionalEnum: PropTypes.oneOf(['News', 'Photos']),
6767

68-
// An object that could be one of many types
68+
// Un objecto que puede ser de diferentes tipos
6969
optionalUnion: PropTypes.oneOfType([
7070
PropTypes.string,
7171
PropTypes.number,
7272
PropTypes.instanceOf(Message)
7373
]),
7474

75-
// An array of a certain type
75+
// Un array de determinado tipo
7676
optionalArrayOf: PropTypes.arrayOf(PropTypes.number),
7777

78-
// An object with property values of a certain type
78+
// Un objecto con valores de propiedad de determinado tipo
7979
optionalObjectOf: PropTypes.objectOf(PropTypes.number),
8080

81-
// An object taking on a particular shape
81+
// Un objecto que tenga determinada estructura
8282
optionalObjectWithShape: PropTypes.shape({
8383
color: PropTypes.string,
8484
fontSize: PropTypes.number
8585
}),
8686

87-
// You can chain any of the above with `isRequired` to make sure a warning
88-
// is shown if the prop isn't provided.
87+
// Puedes encadenar cualquiera de los anteriores con `isRequired` para asegurar
88+
// que se muestre una advertencia si la prop no se suministra.
8989
requiredFunc: PropTypes.func.isRequired,
9090

91-
// A value of any data type
91+
// Un valor de cualquier tipo
9292
requiredAny: PropTypes.any.isRequired,
9393

94-
// You can also specify a custom validator. It should return an Error
95-
// object if the validation fails. Don't `console.warn` or throw, as this
96-
// won't work inside `oneOfType`.
94+
// También puedes suministrar un validador personalizado. Debe retornar un objeto Error
95+
// si la validación falla. No uses `console.warn` o throw, porque no va a funcionar en
96+
// `oneOfType`
9797
customProp: function(props, propName, componentName) {
9898
if (!/matchme/.test(props[propName])) {
9999
return new Error(
@@ -103,11 +103,10 @@ MyComponent.propTypes = {
103103
}
104104
},
105105

106-
// You can also supply a custom validator to `arrayOf` and `objectOf`.
107-
// It should return an Error object if the validation fails. The validator
108-
// will be called for each key in the array or object. The first two
109-
// arguments of the validator are the array or object itself, and the
110-
// current item's key.
106+
// También puedes suministrar un validador personalizado a `arrayOf` y `objectOf`.
107+
// Debe retornar un objeto Error si la validación falla. El validador se llamará
108+
// por cada key en el array o el objecto. Los primeros dos arguments del validador
109+
// son el array o el objeto, y la key del elemento actual.
111110
customArrayProp: PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {
112111
if (!/matchme/.test(propValue[key])) {
113112
return new Error(
@@ -119,16 +118,16 @@ MyComponent.propTypes = {
119118
};
120119
```
121120

122-
### Requiring Single Child
121+
### Solicitar un Sólo Hijo
123122

124-
With `PropTypes.element` you can specify that only a single child can be passed to a component as children.
123+
Usando `PropTypes.element` puedes especificar que únicamente un hijo se pase al componente.
125124

126125
```javascript
127126
import PropTypes from 'prop-types';
128127

129128
class MyComponent extends React.Component {
130129
render() {
131-
// This must be exactly one element or it will warn.
130+
// Debe ser exactamente un elemento o generará una advertencia
132131
const children = this.props.children;
133132
return (
134133
<div>
@@ -143,9 +142,9 @@ MyComponent.propTypes = {
143142
};
144143
```
145144

146-
### Default Prop Values
145+
### Valores por defecto de Props
147146

148-
You can define default values for your `props` by assigning to the special `defaultProps` property:
147+
Puedes definir los valores por defecto de tus props al asignar la propiedad especial `defaultProps`:
149148

150149
```javascript
151150
class Greeting extends React.Component {
@@ -156,19 +155,19 @@ class Greeting extends React.Component {
156155
}
157156
}
158157

159-
// Specifies the default values for props:
158+
// Especifica los valores por defecto de props:
160159
Greeting.defaultProps = {
161160
name: 'Stranger'
162161
};
163162

164-
// Renders "Hello, Stranger":
163+
// Renderiza "Hello, Stranger":
165164
ReactDOM.render(
166165
<Greeting />,
167166
document.getElementById('example')
168167
);
169168
```
170169

171-
If you are using a Babel transform like [transform-class-properties](https://babeljs.io/docs/plugins/transform-class-properties/) , you can also declare `defaultProps` as static property within a React component class. This syntax has not yet been finalized though and will require a compilation step to work within a browser. For more information, see the [class fields proposal](https://github.com/tc39/proposal-class-fields).
170+
Si estás usando un transform de Babel como [transform-class-properties](https://babeljs.io/docs/plugins/transform-class-properties/), puedes declarar `defaultProps` como una propiedad estática al interior de un componente clase de React. Esta sintaxis no se ha terminado y require un paso de compilación para funcionar en el navegador. Para mas información, puedes ver [class field proposal](https://github.com/tc39/proposal-class-fields).
172171

173172
```javascript
174173
class Greeting extends React.Component {
@@ -184,4 +183,4 @@ class Greeting extends React.Component {
184183
}
185184
```
186185

187-
The `defaultProps` will be used to ensure that `this.props.name` will have a value if it was not specified by the parent component. The `propTypes` typechecking happens after `defaultProps` are resolved, so typechecking will also apply to the `defaultProps`.
186+
`defaultProps` se usa para asegurar que `this.props.name` tendrá un valor si no fue especificado por el componente padre. La verificación de tipo de `propTypes` sucede después de resolver `defaultProps`, así que la verificación de tipo también se aplica a `defaultProps`.

0 commit comments

Comments
 (0)