我使用下面的代码在React组件上设置默认道具,但它不起作用。在该render()
方法中,我可以看到输出“ undefined props”已打印在浏览器控制台上。如何为组件道具定义默认值?
export default class AddAddressComponent extends Component {
render() {
let {provinceList,cityList} = this.props
if(cityList === undefined || provinceList === undefined){
console.log('undefined props')
}
...
}
AddAddressComponent.contextTypes = {
router: React.PropTypes.object.isRequired
}
AddAddressComponent.defaultProps = {
cityList: [],
provinceList: [],
}
AddAddressComponent.propTypes = {
userInfo: React.PropTypes.object,
cityList: PropTypes.array.isRequired,
provinceList: PropTypes.array.isRequired,
}
对于功能类型prop,可以使用以下代码: