React propTypes - slahsk/study GitHub Wiki

속성 μœ νš¨μ„± 검사

μ»΄ν¬λ„ŒνŠΈμ— μ–΄λ–€ 속성을 μ‚¬μš©ν•  수 있고, μ–΄λ–€ 속성이 ν•„μˆ˜ 이며, μ–΄λ–€ ν˜•μ‹μ˜ 값을 λ°›λŠ”μ§€ 등을 λͺ…μ‹œμ μœΌλ‘œ μ§€μ •ν•˜λŠ” 것이 μ’‹λ‹€. 이λ₯Ό μœ„ν•΄μ„œ protoType λ₯Ό μ„ μ–Έν•œλ‹€.

  • μ–Έμ œλ“ μ§€ μ»΄ν¬λ„ŒνŠΈλ₯Ό μ—΄κ³  μ–΄λ–€ 속성이 ν•„μš”ν•œμ§€, 그리고 μ†μ„±μ˜ ν˜•μ‹μ΄ 무엇인지 μ•Œ 수 μžˆλ‹€.
  • λ¬Έμ œκ°€ λ°œμƒν•  경우 λ¦¬μ—‘νŠΈκ°€ μ½˜μ†”μ— 였λ₯˜ λ©”μ‹œμ§€λ₯Ό 좜λ ₯ν•΄ μ–΄λ–€ 속성이 잘λͺ» λ˜κ±°λ‚˜ λˆ„λ½λλŠ”μ§€ , 그리고 λ¬Έμ œκ°€ λ°œμƒν•œ render λ©”μ„œλ“œκ°€ 무엇인지 μ•Œλ €μ€„ 수 μžˆλ‹€.

기본으둜 μ œκ³΅λ˜λŠ” PropTypes

μœ νš¨μ„± 검사기 μ„€λͺ…
PropTypes.array 속성이 배열이야 ν•œλ‹€.
PropTypes.bool 속성이 λΆ€μšΈ κ°’(true/false) 이어야 ν•œλ‹€.
PropTypes.func 속성이 ν•¨μˆ˜μ—¬μ•Ό ν•œλ‹€.
PropTypes.number 속성이 숫자(λ˜λŠ” ꡬ문 λΆ„μ„μœΌλ‘œ 숫자λ₯Ό 얻을 수 μžˆλŠ” κ°’) 이어야 ν•œλ‹€.
PropTypes.object 속성이 객체여야 ν•œλ‹€.
PropTypes.string 속성이 λ¬Έμžμ—΄μ΄μ•Ό ν•œλ‹€.
μœ νš¨μ„± 검사기 μ„€λͺ…
PropTypes.oneOfType 속성이 μ—¬λŸ¬ ν˜•μ‹ 쀑 ν•˜λ‚˜μΌ 수 μžˆλ‹€.
예:
PropTypes.oneOfType([
 PropTypes.string,
 PropTypes.number
 PropTypes.instanceOf(Message)
])
PropTypes.arryOf 속성이 νŠΉμ • 배열이야 ν•œλ‹€.
예) PropTypes.arrayOf(PropTypes.number)
PropTypes.objectOf 속성이 νŠΉμ • ν˜•μ‹μ˜ 속성 값을 κ°€μ§„ 객체여야 ν•œλ‹€.
예) PropTypes.objectOf(PropTypes.number)
PropTypes.shape μ†μ„±μ˜ νŠΉμ • ν˜•νƒœλ₯Ό κ°€μ§„ 객체여야 ν•œλ‹€.
PropTypes.shape({
 color:PropTypes.string,
 fontSzie: PropTypes.number
})
PropTypes.node 속성이 λ Œλ”λ§ κ°€λŠ₯ν•œ μ–΄λ–€ 값이라도 될 μˆ˜μžˆλ‹€(숫자, 문자, μš”μ†Œ, λ°°μ—΄)
PropTypes.element 속성이 λ¦¬μ—‘νŠΈ μš”μ†Œμ—¬μ•Ό ν•œλ‹€.
PropTypes.instanceof 속성이 μ§€μ •ν•œ 클래슀의 μΈμŠ€ν„΄μŠ€μ—¬μ—¬ ν•œλ‹€(μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ instanceof μ—°μ‚°μžλ₯Ό μ΄μš©ν•œλ‹€.)
예: PropTypes.instanceOf(Message)
PropTypes.oneOf 속성이 μ—΄κ±°ν˜•κ³Ό 같이 νŠΉμ •ν•œ λ²”μœ„μ˜ κ°’μœΌλ‘œ ν—Œμ •λΌμ•Ό ν•œλ‹€.
예: PropTypes.oneOf(['News','Photos'])
import React, {Component,PropTypes} from 'react';
import Card from './Card';
import style from './kanban.css';

class List extends Component {
    render() {
        var cards = this.props.cards.map((card, i) => {
            return <Card key={card.id} id={card.id} color={card.color} title={card.title} description={card.description} tasks={card.tasks}/>;
        });

        return (
            <div className={style.list}>
                <h1>{this.props.title}</h1>
                {cards}
            </div>
        );
    }
};

List.propTypes = {
  title: PropTypes.string.isRequired,
  cards : PropTypes.arrayOf(PropTypes.object)
};

export default List;

propType Custom

import React,{Component, PropTypes} from 'react';
import CheckList from './CheckList';
import marked from 'marked';
import style from './kanban.css';

let titlePropType  = (props, propName, ComponentName) => {
  if(props[propName]){
    let value = props[propName];

    if(typeof value !== 'string' || value.length > 80){
      return Error('${propName} in ${ComponentName} is longer than 80 character');
    }
  }
};

class Card extends Component{
  ...
    return (
    ...
    );
  }
};

Card.propTypes = {
  id : PropTypes.number,
  title: titlePropType,
  description : PropTypes.string,
  color : PropTypes.string,
  tasks : PropTypes.arrayOf(PropTypes.object)
};

export default Card;
⚠️ **GitHub.com Fallback** ⚠️