緣由
大型應(yīng)用程序的構(gòu)建更適合用強(qiáng)類型的語言來構(gòu)建,它有更多的規(guī)則,可以幫助我們?cè)诰帉懘a階段、編譯階段規(guī)避掉很多問題,讓我們的應(yīng)用程序更加的安全。
使用方法
import React, { Component } from 'react'
import PropTypes from 'prop-types'
class Comment extends Component {
static propTypes = {
comment: PropTypes.object
}
render () {
const { comment } = this.props
return (
<div className='comment'>
<div className='comment-user'>
<span>{comment.username} </span>:
</div>
<p>{comment.content}</p>
</div>
)
}
}
組件參數(shù)驗(yàn)證在構(gòu)建大型的組件庫的時(shí)候相當(dāng)有用,可以幫助我們迅速定位這種類型錯(cuò)誤,讓我們組件開發(fā)更加規(guī)范。
更多使用案例:typechecking-with-proptypes