国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

characters

注意:   React.PropTypes自從React v15.5以來,它已經(jīng)進入了一個不同的包。請使用該prop-types庫,而不是。我們提供了一個codemod腳本來自動化轉(zhuǎn)換。

隨著您的應用程序的增長,您可以通過類型檢查來捕捉大量錯誤。對于某些應用程序,您可以使用JavaScript擴展(如Flow或TypeScript)來檢查整個應用程序。但即使你不使用這些,React也有一些內(nèi)置的類型檢測功能。要在組件的道具上運行類型檢查,可以指定特殊propTypes屬性:

import PropTypes from 'prop-types';
class Greeting extends React.Component {  
    render() {    return (      <h1>Hello, {this.props.name}</h1>    );  }}
Greeting.propTypes = {
  name: PropTypes.string};

PropTypes導出一系列可用于確保您收到的數(shù)據(jù)有效的驗證程序。在這個例子中,我們正在使用PropTypes.string。當為prop提供無效值時,JavaScript控制臺中將顯示警告。出于性能原因,propTypes僅在開發(fā)模式下進行檢查。

PropTypes

這里是一個記錄提供的不同驗證器的例子:

import PropTypes from 'prop-types';
MyComponent.propTypes = {  // You can declare that a prop is a specific JS primitive. By default, these  // are all optional.
  optionalArray: PropTypes.array,
  optionalBool: PropTypes.bool,
  optionalFunc: PropTypes.func,
  optionalNumber: PropTypes.number,
  optionalObject: PropTypes.object,
  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.
  optionalElement: PropTypes.element,  // You can also declare that a prop is an instance of a class. This uses  // JS's instanceof operator.
  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.
  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`.
  customProp: function(props, propName, componentName) {    if (!/matchme/.test(props[propName])) {      return new Error(
        'Invalid prop `' + propName + '` supplied to' +
        ' `' + componentName + '`. Validation failed.'      );    }  },  // 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.
  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.'      );    }  })};

需要單個孩子

隨著PropTypes.element您可以指定只有一個孩子可以傳遞給兒童一個組成部分。

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>        {children}      </div>    );  }}
MyComponent.propTypes = {
      children: PropTypes.element.isRequired
  };

默認支柱值

您可以props通過分配特殊defaultProps屬性來為您定義默認值:

class Greeting extends React.Component {  
    render() {    return (      <h1>Hello, {this.props.name}</h1>    );  }}
    // Specifies the default values for props:
    Greeting.defaultProps = {
  name: 'Stranger'};// Renders "Hello, Stranger":ReactDOM.render(  <Greeting />,
  document.getElementById('example'));

如果使用類似transform-class-properties的Babel變換,則還可以defaultProps在React組件類中聲明為靜態(tài)屬性。盡管這個語法尚未完成,并且需要編譯步驟才能在瀏覽器中工作。有關(guān)更多信息,請參閱班級字段提案。

class Greeting extends React.Component {  
    static defaultProps = {
        name: 'stranger'  }  
    render() {    return (      <div>Hello, {this.props.name}</div>    )  }}

defaultProps將被用于確保this.props.name,如果不是由父組件指定它將有一個值。類型檢查propTypes發(fā)生在defaultProps解決后,因此類型檢查也將適用于該類型defaultProps。

Previous article: Next article: