-
Notifications
You must be signed in to change notification settings - Fork 391
Update 2013-07-11-react-v0-4-prop-validation-and-default-values.md #554
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,37 +1,48 @@ | ||||||
--- | ||||||
title: "New in React v0.4: Prop Validation and Default Values" | ||||||
title: "Новое в React v0.4: Валидацяи пропсов и значения по умолчанию" | ||||||
author: [zpao] | ||||||
--- | ||||||
|
||||||
Many of the questions we got following the public launch of React revolved around `props`, specifically that people wanted to do validation and to make sure their components had sensible defaults. | ||||||
Многие из вопросов, которые мы получили после публичного запуска React, | ||||||
касались пропсов, в частности, того, что люди хотели провести валидацию | ||||||
и убедиться в том, что их компоненты имеют верные значения по умолчанию. | ||||||
|
||||||
|
||||||
## Validation {#validation} | ||||||
## Валидация {#валидация} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Идентификаторы разделов не нужно переводить, так как на них ссылаются в других статьях, в которых они не переводились. Подробнее можно почитать здесь |
||||||
|
||||||
Oftentimes you want to validate your `props` before you use them. Perhaps you want to ensure they are a specific type. Or maybe you want to restrict your prop to specific values. Or maybe you want to make a specific prop required. This was always possible — you could have written validations in your `render` or `componentWillReceiveProps` functions, but that gets clunky fast. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В вашем переводе осталась часть текста на английском языке. Пожалуйста, проверяйте свои изменения перед созданием пулл реквеста. |
||||||
|
||||||
React v0.4 will provide a nice easy way for you to use built-in validators, or to even write your own. | ||||||
Часто вы хотите проверить ваши пропсы перед тем, как использовать их. | ||||||
Возможно, вы хотите удостовериться, что они относятся к определенному типу. | ||||||
Или, может быть, вы хотите ограничить ваш пропсы конкретными значениями. | ||||||
Или, может быть, вы хотите сделать требуемый пропс. Это всегда было | ||||||
возможно - вы могли написать валидацию в `render` или функции | ||||||
`componentWillReceiveProps`, но это быстро становится неудобным. | ||||||
|
||||||
React v0.4 предоставит вам хороший простой способ использовать встроенные | ||||||
валидаторы или даже написать свои собственные. | ||||||
|
||||||
```js | ||||||
React.createClass({ | ||||||
propTypes: { | ||||||
// An optional string prop named "description". | ||||||
// Опциональная строковая пропса с именем "description". | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
В нашем согласовании перевода приняли, что Пожалуйста, ознакомьтесь с нашим соглашением по переводу, он находится здесь. |
||||||
description: React.PropTypes.string, | ||||||
|
||||||
// A required enum prop named "category". | ||||||
// Требуемая перечисляемая пропса с именем "category". | ||||||
category: React.PropTypes.oneOf(['News','Photos']).isRequired, | ||||||
|
||||||
// A prop named "dialog" that requires an instance of Dialog. | ||||||
// Пропса с именем "dialog" которая должна соответстовать типу Dialog. | ||||||
dialog: React.PropTypes.instanceOf(Dialog).isRequired | ||||||
}, | ||||||
... | ||||||
}); | ||||||
``` | ||||||
|
||||||
|
||||||
## Default Values {#default-values} | ||||||
## Значения по умолчанию {#значения-по-умолчанию} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Идентификаторы разделов не нужно переводить, так как на них ссылаются в других статьях, в которых они не переводились. Подробнее можно почитать здесь |
||||||
|
||||||
One common pattern we've seen with our React code is to do something like this: | ||||||
Один общий шаблон, который мы видели с нашим React кодом, это сделать | ||||||
что-то вроде этого: | ||||||
|
||||||
```js | ||||||
React.createClass({ | ||||||
|
@@ -42,7 +53,9 @@ React.createClass({ | |||||
}); | ||||||
``` | ||||||
|
||||||
Do this for a few `props` across a few components and now you have a lot of redundant code. Starting with React v0.4, you can provide default values in a declarative way: | ||||||
Сделайте это для нескольких пропсов в некоторых компонентам и теперь у | ||||||
вас будет много избыточного кода. Начиная с React v0.4, вы можете | ||||||
предоставлять значения по умолчанию декларативным способом: | ||||||
|
||||||
```js | ||||||
React.createClass({ | ||||||
|
@@ -55,8 +68,12 @@ React.createClass({ | |||||
}); | ||||||
``` | ||||||
|
||||||
We will use the cached result of this function before each `render`. We also perform all validations before each `render` to ensure that you have all of the data you need in the right form before you try to use it. | ||||||
Мы будем использовать кэшированный результат этой функции перед каждым `render`. | ||||||
Мы также выполняем все проверки перед каждым `render`, чтобы удостовериться, | ||||||
что у вас есть все необходимые данные в правильной форме, прежде чем вы попытаетесь их использовать. | ||||||
|
||||||
- - - | ||||||
|
||||||
Both of these features are entirely optional. We've found them to be increasingly valuable at Facebook as our applications grow and evolve, and we hope others find them useful as well. | ||||||
Обе эти функции являются полностью опциональными. Мы обнаружили, что | ||||||
они становятся все более ценными на Facebook по мере роста и развития | ||||||
наших приложений, и надеемся, что другие тоже найдут их полезными. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Старайтесь сохранять нумерацию строк в оригинале и в ваших переводах. Это поможет избежать конфликтов при обновлении документации из официального репозитория.