diff --git a/content/docs/nav.yml b/content/docs/nav.yml index ce5219f92..465c2f3dc 100644 --- a/content/docs/nav.yml +++ b/content/docs/nav.yml @@ -74,7 +74,7 @@ - id: strict-mode title: Strict Mode - id: typechecking-with-proptypes - title: Typechecking With PropTypes + title: Проверка типов с помощью PropTypes - id: uncontrolled-components title: Uncontrolled Components - id: web-components diff --git a/content/docs/typechecking-with-proptypes.md b/content/docs/typechecking-with-proptypes.md index 4004e7820..c3e6eeb66 100644 --- a/content/docs/typechecking-with-proptypes.md +++ b/content/docs/typechecking-with-proptypes.md @@ -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). > ->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), чтобы провести замену в коде на использование этой библиотеки. -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: +По мере роста вашего приложения вы можете отловить много ошибок с помощью проверки типов. Для этого можно использовать расширения JavaScript вроде [Flow](https://flow.org/) и [TypeScript](https://www.typescriptlang.org/). Но, даже если вы ими не пользуетесь, React предоставляет встроенные возможности для проверки типов. Для запуска этой проверки на пропсах компонента вам нужно использовать специальное свойство `propTypes`: ```javascript import PropTypes from 'prop-types'; @@ -20,7 +20,7 @@ import PropTypes from 'prop-types'; class Greeting extends React.Component { render() { return ( -

Hello, {this.props.name}

+

Привет, {this.props.name}

); } } @@ -30,18 +30,18 @@ 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`. Когда какой-то проп имеет некорректное значение, в консоли будет выведено предупреждение. По соображениям производительности `propTypes` проверяются только в режиме разработки. ### PropTypes {#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-типу. + // По умолчанию это не обязательно. optionalArray: PropTypes.array, optionalBool: PropTypes.bool, optionalFunc: PropTypes.func, @@ -50,85 +50,90 @@ MyComponent.propTypes = { optionalString: PropTypes.string, optionalSymbol: PropTypes.symbol, - // Anything that can be rendered: numbers, strings, elements or an array - // (or fragment) containing these types. + // Все, что может быть отренедерено: + // числа, строки, элементы или массивы + // (или фрагменты) содержащие эти типы optionalNode: PropTypes.node, - // A React element. + // React-элемент optionalElement: PropTypes.element, - // You can also declare that a prop is an instance of a class. This uses - // JS's instanceof operator. + // Можно указать, что проп должен быть экземпляром класса + // Для этого используется оператор `instanceof`. optionalMessage: PropTypes.instanceOf(Message), - // You can ensure that your prop is limited to specific values by treating - // it as an enum. + // Вы можете задать ограничение конкретными значениями + // при помощи перечисления optionalEnum: PropTypes.oneOf(['News', 'Photos']), - // An object that could be one of many types + // Объект, одного из нескольких типов optionalUnion: PropTypes.oneOfType([ PropTypes.string, PropTypes.number, PropTypes.instanceOf(Message) ]), - // An array of a certain type + // Массив объектов конкретного типа optionalArrayOf: PropTypes.arrayOf(PropTypes.number), - // An object with property values of a certain type + // Объект со свойствами конкретного типа optionalObjectOf: PropTypes.objectOf(PropTypes.number), - // An object taking on a particular shape + // Объект с определенной структурой 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. + // Можно добавить`isRequired` к любому из приведённому выше типу, + // чтобы показывать предупреждение, + // если проп не передан 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` или `throw` + // - это не будет работать внутри `oneOfType` customProp: function(props, propName, componentName) { if (!/matchme/.test(props[propName])) { return new Error( - 'Invalid prop `' + propName + '` supplied to' + - ' `' + componentName + '`. Validation failed.' + 'Проп `' + propName + '` компонента' + + ' `' + componentName + '` имеет неправильное значение' ); } }, - // 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 при ошибке валидации. + // Валидатор будет вызван для каждого элемента в массиве + // или для каждого свойства объекта. + // Первые два параметра валидатора + // - это массив или объект и ключ текущего элемента customArrayProp: PropTypes.arrayOf(function(propValue, key, componentName, location, propFullName) { if (!/matchme/.test(propValue[key])) { return new Error( - 'Invalid prop `' + propFullName + '` supplied to' + - ' `' + componentName + '`. Validation failed.' + 'Проп `' + propFullName + '` компонента' + + ' `' + componentName + '` имеет неправильное значение' ); } }) }; ``` -### Requiring Single Child {#requiring-single-child} +### Ограничение на один дочерний компонент {#requiring-single-child} -With `PropTypes.element` you can specify that only a single child can be passed to a component as children. +С помощью `PropTypes.element` вы можете указать, что в качестве дочернего может быть передан только один элемент. ```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 (
@@ -143,45 +148,45 @@ MyComponent.propTypes = { }; ``` -### Default Prop Values {#default-prop-values} +### Значения пропсов по умолчанию {#default-prop-values} -You can define default values for your `props` by assigning to the special `defaultProps` property: +Вы можете задать значения по умолчанию для ваших `props` с помощью специального свойства `defaultProps`: ```javascript class Greeting extends React.Component { render() { return ( -

Hello, {this.props.name}

+

Привет, {this.props.name}

); } } -// Specifies the default values for props: +// Задание значений по умолчанию для пропсов: Greeting.defaultProps = { - name: 'Stranger' + name: 'Незнакомец' }; -// Renders "Hello, Stranger": +// Отрендерит "Привет, Незнакомец": ReactDOM.render( , 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). +Если вы используете один из Babel-плагинов по преобразованию кода, вроде [transform-class-properties](https://babeljs.io/docs/plugins/transform-class-properties/), то можете объявить `defaultProps` как статическое свойство класса (для компонента-наследника от `React.Component`). Этот синтаксис еще не утвержден, так что для его работы в браузере нужна компиляция. Подробнее смотрите в [предложении о полях класса](https://github.com/tc39/proposal-class-fields). ```javascript class Greeting extends React.Component { static defaultProps = { - name: 'stranger' + name: 'незнакомец' } render() { return ( -
Hello, {this.props.name}
+
Привет, {this.props.name}
) } } ``` -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` будет иметь значение, даже если оно не было указано родительским компонентом. Сначала применяются значения по умолчанию, заданные в `defaultProps`. После запускается проверка типов с помощью `propTypes`. Так что проверка типов распространяется и на значения по умолчанию.