티스토리 뷰
728x90
반응형
데이터를 받을때 그데이터의 타입이 잘못된것인지 확인할 필요가있다
import PropTypes from 'prop-types'
class Movie extends Component {
static propTypes = {
title: PropTypes.string.isRequired, // PropTypes가 기본으로 있는 타입이아니라서 에러가남 타입을 check를 함 데이터통신하다 이상한 타입이들어올수있으니
poster: PropTypes.string
};
render(){ // html을 리턴해주는놈
return(
<div className='Movie'>
<h1>{this.props.title}</h1>
<MoviePoster poster={this.props.poster}/>
</div>
);
}
}
propTypes를 이용하여 받을 데이터의 타입을 지정해줄수있다 string이 아닌 다른 데이터타입이 오면 에러가 발생
isRequired를 붙여주면 데이터가 null이 되어서는 안된다
PropTypes는 기본적으로 설치가 되어 있지않기 때문에 설치해주어야한다
npm install prop-types
728x90
반응형
'Error > React.js' 카테고리의 다른 글
[React] Typo in static class property declaration react/no-typos (3) | 2019.09.01 |
---|---|
[React] index.js:1375 Warning: The tag <plus> is unrecognized in this browser. If you meant to render a React component, start its name with an uppercase letter. (0) | 2019.08.18 |
static state 에러 (0) | 2019.05.01 |
Warning key (0) | 2019.04.15 |
npm install -g create-react-app Error (0) | 2019.04.15 |