Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs Typechecking With PropTypes #108

Merged
merged 5 commits into from
Mar 27, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 37 additions & 41 deletions content/docs/typechecking-with-proptypes.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
---
id: typechecking-with-proptypes
title: Typechecking With PropTypes
title: 使用 PropTypes 进行类型检查
permalink: docs/typechecking-with-proptypes.html
redirect_from:
- "docs/react-api.html#typechecking-with-proptypes"
---

> Note:
> 注意:
>
> `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).
> React 15.5 起,`React.PropTypes` 已移入另一个包中。请使用 [`prop-types`](https://www.npmjs.com/package/prop-types) 库代替。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

自 React 15.5 起 => 自 React v15.5 起,版本v我觉得还是保留比较好吧~

如果要与原文一致的话,写成 请使用prop-types 库替代,会不会更好呢?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉不需要翻译出来。

>
>We provide [a codemod script](/blog/2017/04/07/react-v15.5.0.html#migrating-from-reactproptypes) to automate the conversion.
>我们提供了一个 [codemod](/blog/2017/04/07/react-v15.5.0.html#migrating-from-reactproptypes) 脚本来自动转换。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里也是一样的问题,可以把 一个 codemon 脚本 放在链接内吧。这样以后原文更改后这里的翻译变化能更对应~


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:
随着你的应用程序不断增长,你可以通过类型检查捕获大量错误。对于某些应用程序来说,你可以使用 [Flow](https://flow.org/) [TypeScript](https://www.typescriptlang.org/) 等 JavaScript 扩展来对整个应用程序做类型检查。但即使你不使用这些扩展,React 也内置了一些类型检查的功能。要在组件的 props 上进行类型检查,你只需配置特定的 `propTypes` 属性:

```javascript
import PropTypes from 'prop-types';
Expand All @@ -30,18 +30,17 @@ Greeting.propTypes = {
};
```

`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.
`PropTypes` 提供一系列验证器,可用于组件接收的数据类型是有效的。在本例中, 我们使用了 `PropTypes.string`。当传入类型不正确的属性时,JavaScript 控制台将会显示警告。出于性能方面的考虑,`propTypes` 只仅在开发模式下进行检查。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 可用于组件接收的数据类型是有效的 =》 可用于确保组件接收的数据类型是有效的。忘记翻译啦~

  2. 当传入类型不正确的属性时 =》 当一个类型不正确的值传入该 prop 时。 我个人倾向于 prop 不翻译(之前貌似讨论的是这样的),其次我觉得这里应该把“value”翻译出来吧~

  3. 只仅在开发模式下进行检查 =》 仅在开发模式下被检查

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

会不会有点牵强


### PropTypes {#proptypes}
### Prop 类型 {#proptypes}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PropTypes 是专有名词吧,这里不应该拆开来翻译,你觉得呢?


Here is an example documenting the different validators provided:
以下提供了使用不同验证器的例子:

```javascript
import PropTypes from 'prop-types';

MyComponent.propTypes = {
// You can declare that a prop is a specific JS type. By default, these
// are all optional.
// 你可以将属性声明为 JS 原生类型,默认情况下这些属性都是可选的。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里根据之前群里讨论的结果,要求中文行数要和英文一致,防止错位,使得后续 merge 有困难。

optionalArray: PropTypes.array,
optionalBool: PropTypes.bool,
optionalFunc: PropTypes.func,
Expand All @@ -50,50 +49,47 @@ MyComponent.propTypes = {
optionalString: PropTypes.string,
optionalSymbol: PropTypes.symbol,

// Anything that can be rendered: numbers, strings, elements or an array
// (or fragment) containing these types.
// 任何可被渲染的元素(包括数字、字符串、子元素或数组)
//(或 Fragment)也包含这些类型。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 括号前面也要加个空格吧~ 注释符号后面都要留空。

  2. 原文用了冒号,这里也用冒号应该也不会有问题;而且现在用括号会变成“(包括数字、字符串、子元素或数组)(或 Fragment)”,这样一看是不是就很奇怪了?所以还是按原文的标点来即可。

  3. “子元素”的翻译还要斟酌一下,毕竟 props 中传入的 element 不一定作为“子”元素。

optionalNode: PropTypes.node,

// A React element.
// 一个 React 元素
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

末尾跟上句号以保持一致?

optionalElement: PropTypes.element,

// You can also declare that a prop is an instance of a class. This uses
// JS's instanceof operator.
// 你也可以声明属性为某个类的实例,这里使用 JS 的 instanceof 操作符。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里也有行数不对应问题,现在的文章已经和原文有两行不一致了。

还有关于 prop 到底要不要翻译,建议去群里讨论一下~

optionalMessage: PropTypes.instanceOf(Message),

// You can ensure that your prop is limited to specific values by treating
// it as an enum.
// 你可以使你的属性只接收指定的值。
// 这是一个枚举类型。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里貌似为了行数对应翻译成了两句话?直接翻译成一句话但是中间换行可不可以呢?

你可以通过将 prop 设定为枚举类型来确保它只能是一些特定的值。

optionalEnum: PropTypes.oneOf(['News', 'Photos']),

// An object that could be one of many types
// 指定的多个对象类型中的一个
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

umm,感觉这里翻译的不是很清楚呀~

一个对象可以是几种类型中的任意一个类型。怎么样呢?

我个人觉得把这里的名词短语翻译成语句会更合适(包括下面的几条),毕竟英文更倾向于用名词形容事物,而中文却很少用名词描述。

optionalUnion: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
PropTypes.instanceOf(Message)
]),

// An array of a certain type
// 一个指定类型组成的数组
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果按你的翻译模式,可以是“一个由某一类型的元素组成的数组”

如果按我上面的建议,可以是“可以指定一个数组由某一类型的元素组成”

optionalArrayOf: PropTypes.arrayOf(PropTypes.number),

// An object with property values of a certain type
// 一个指定类型属性构成的对象
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里我觉得也是可以翻译地更清楚的,直译的话名词堆叠在一起看一眼很难理解。

optionalObjectOf: PropTypes.objectOf(PropTypes.number),

// An object taking on a particular shape
// 一个指定属性及其类型的对象
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

optionalObjectWithShape: PropTypes.shape({
color: PropTypes.string,
fontSize: PropTypes.number
}),

// You can chain any of the above with `isRequired` to make sure a warning
// is shown if the prop isn't provided.
// 你可以在任何 PropTypes 属性后面加上 `isRequired` ,确保这个属性父组件没有提供时,会打印警告信息。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 行数

  2. “这个属性父组件没有提供”貌似有点歧义?
    直接“这个 prop 没有被提供”怎么样呢?

requiredFunc: PropTypes.func.isRequired,

// A value of any data type
// 任意类型的数据
requiredAny: PropTypes.any.isRequired,

// You can also specify a custom validator. It should return an Error
// object if the validation fails. Don't `console.warn` or throw, as this
// won't work inside `oneOfType`.
// 你可以指定一个自定义验证器。如果验证失败应该返回一个 Error 对象
// 而不是 `console.warn` 或抛异常,因为在 `onOfType` 中不会起作用。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你可以指定一个自定义验证器。它在验证失败时应返回一个 Error 对象。请不要使用 console.warn 或者抛出异常,因为这在 onOfType 中不会起作用。

我觉得这样直译就可以了。

注意行数~

customProp: function(props, propName, componentName) {
if (!/matchme/.test(props[propName])) {
return new Error(
Expand All @@ -103,11 +99,10 @@ MyComponent.propTypes = {
}
},

// You can also supply a custom validator to `arrayOf` and `objectOf`.
// It should return an Error object if the validation fails. The validator
// will be called for each key in the array or object. The first two
// arguments of the validator are the array or object itself, and the
// current item's key.
// 你也可以提供一个自定义的 `arrayOf` 或 `objectOf` 验证器。
// 它应该在验证失败时返回一个 Error 对象。
// 验证器将验证数组或对象中的每个值。验证器的前两个参数
// 第一个是数组或对象本身,第二个是他们的键。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

验证器的前两个参数分别是数组或者对象自身和当前元素的键。

current 翻译出来可能更好~

注意行数

customArrayProp: PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) {
if (!/matchme/.test(propValue[key])) {
return new Error(
Expand All @@ -119,16 +114,17 @@ MyComponent.propTypes = {
};
```

### Requiring Single Child {#requiring-single-child}
### 限制一个元素 {#requiring-single-child}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在标题中,使用“限制单个元素”是否更正式些呢?


With `PropTypes.element` you can specify that only a single child can be passed to a component as children.
使用 `PropTypes.element`
你可以指定 children 只有一个元素作为传递到组件。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

行数问题。

额,这里是机器翻译吗?
翻译成“你可以通过 PropTypes.element 来确保传递给组件的 children 中只包含一个子元素”。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

子元素 这里感觉不太合适,本意是一个 ReactElement。


```javascript
import PropTypes from 'prop-types';

class MyComponent extends React.Component {
render() {
// This must be exactly one element or it will warn.
// 这必须只有一个元素,否则控制台会打印警告。
const children = this.props.children;
return (
<div>
Expand All @@ -143,9 +139,9 @@ MyComponent.propTypes = {
};
```

### Default Prop Values {#default-prop-values}
### 属性默认值 {#default-prop-values}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

占个坑~
主要是 prop 是否翻译的问题。
如果不翻译,那么是“默认 prop 值”


You can define default values for your `props` by assigning to the special `defaultProps` property:
您可以通过配置特定的 `defaultProps` 属性来定义 `props` 的默认值:

```javascript
class Greeting extends React.Component {
Expand All @@ -156,19 +152,19 @@ class Greeting extends React.Component {
}
}

// Specifies the default values for props:
// 指定 props 的默认值:
Greeting.defaultProps = {
name: 'Stranger'
};

// Renders "Hello, Stranger":
// 渲染出 "Hello, Stranger"
ReactDOM.render(
<Greeting />,
document.getElementById('example')
);
```

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).
如果你正在使用像 [transform-class-properties](https://babeljs.io/docs/plugins/transform-class-properties/) 的 Babel 转换工具,你也可以在 React 组件类中声明 `defaultProps` 作为静态属性。此语法提案还没有最终确定,在浏览器中需要进行编译才能使用。要了解更多信息,请查阅 [class fields proposal](https://github.com/tc39/proposal-class-fields)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在浏览器中需要进行编译才能使用 有歧义,因为浏览器不能编译这些新语法。不如翻译成“需要编译后才能在浏览器中运行”


```javascript
class Greeting extends React.Component {
Expand All @@ -184,4 +180,4 @@ class Greeting extends React.Component {
}
```

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`.
`defaultProps` 用于确保 `this.props.name` 在父组件没有指定值时,有一个默认值。`propTypes` 类型检查发生在 `defaultProps` 赋值后,所以类型检查也适用于 `defaultProps`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在父组件没有指定值时 =》 在父组件没有指定其值时

这样会不会更清晰一些~