Programing

형태와 반응 형 배열

lottogame 2020. 4. 23. 07:37
반응형

형태와 반응 형 배열


구성 요소에 전달되는 객체 배열이 실제로 특정 모양의 객체 배열인지 확인하기 위해 proptype을 사용하는 기본 제공 방법이 있습니까?

아마도 이런 식으로 뭔가?

annotationRanges: PropTypes.array(PropTypes.shape({
    start: PropTypes.number.isRequired,
    end: PropTypes.number.isRequired,
})),

여기에 명백한 것이 빠져 있습니까? 이것처럼 많이 찾는 것 같습니다.


다음에 React.PropTypes.shape()대한 인수로 사용할 수 있습니다 React.PropTypes.arrayOf().

// an array of a particular shape.
ReactComponent.propTypes = {
   arrayWithShape: React.PropTypes.arrayOf(React.PropTypes.shape({
     color: React.PropTypes.string.isRequired,
     fontSize: React.PropTypes.number.isRequired,
   })).isRequired,
}

문서 소품 검증 섹션을 참조하십시오 .

최신 정보

현재 react v15.5사용 React.PropTypes은 사용되지 않으며 prop-types대신 독립형 패키지를 사용해야합니다.

// an array of a particular shape.
import PropTypes from 'prop-types'; // ES6 
var PropTypes = require('prop-types'); // ES5 with npm
ReactComponent.propTypes = {
   arrayWithShape: PropTypes.arrayOf(PropTypes.shape({
     color: PropTypes.string.isRequired,
     fontSize: PropTypes.number.isRequired,
   })).isRequired,
}

예, 코드 PropTypes.arrayOf대신 사용해야 PropTypes.array합니다. 다음과 같이 할 수 있습니다.

import PropTypes from 'prop-types';

MyComponent.propTypes = {
  annotationRanges: PropTypes.arrayOf(
    PropTypes.shape({
      start: PropTypes.string.isRequired,
      end: PropTypes.number.isRequired
    }).isRequired
  ).isRequired
}

또한 대한 자세한 내용은 proptypes , 방문 PropTypes으로 유형 검사 여기


그리고 바로 내 코 바로 아래에 있습니다.

반응 문서 자체에서 https://facebook.github.io/react/docs/reusable-components.html

// An array of a certain type
    optionalArrayOf: React.PropTypes.arrayOf(React.PropTypes.number),

ES6 속기 가져 오기가 있습니다. 참조 할 수 있습니다. 더 읽기 쉽고 입력하기 쉽습니다.

import React, { Component } from 'react';
import { arrayOf, shape, number } from 'prop-types';

class ExampleComponent extends Component {
  static propTypes = {
    annotationRanges: arrayOf(shape({
      start: number,
      end: number,
    })).isRequired,
  }

  static defaultProps = {
     annotationRanges: [],
  }
}

특정 모양에 대해 동일한 proptypes를 여러 번 정의하려면 proptypes 파일로 추상화하여 객체의 모양이 변경되면 한 곳에서만 코드를 변경하면됩니다. 코드베이스를 약간 건조시키는 데 도움이됩니다.

예:

// Inside my proptypes.js file
import PT from 'prop-types';

export const product = {
  id: PT.number.isRequired,
  title: PT.string.isRequired,
  sku: PT.string.isRequired,
  description: PT.string.isRequired,
};


// Inside my component file
import PT from 'prop-types';
import { product } from './proptypes;


List.propTypes = {
  productList: PT.arrayOf(product)
}

이것은 빈 배열로부터 보호하는 솔루션이었습니다.

import React, { Component } from 'react';
import { arrayOf, shape, string, number } from 'prop-types';

ReactComponent.propTypes = {
  arrayWithShape: (props, propName, componentName) => {
    const arrayWithShape = props[propName]
    PropTypes.checkPropTypes({ arrayWithShape:
        arrayOf(
          shape({
            color: string.isRequired,
            fontSize: number.isRequired,
          }).isRequired
      ).isRequired
    }, {arrayWithShape}, 'prop', componentName);
    if(arrayWithShape.length < 1){
      return new Error(`${propName} is empty`)
    }
  }
}

참고 URL : https://stackoverflow.com/questions/32325912/react-proptype-array-with-shape

반응형