코드 잡동사니

React default Props 설정, 검증 방법 본문

React

React default Props 설정, 검증 방법

세객 2018. 9. 24. 17:57
class MyComponent extends Component {
    static defaultProps = {
        name : null
    }

    static propTypes = {
        name : PropTypes.string.isRequired
    }

    constructor(props){
        super(props)
    }

    render(){
        return (
            
안녕하세요, 제 이름은 {this.props.name} 입니다.
) } }

defaultProps 는 MyComponent props 초기화 해준다. 

propTypes 는 props 의 데이터 검증하는 것인데 name 이 문자열이고 꼭 입력해야하는 값이라는 것을 나타내고 있다. 

Comments