剛開始學習react,看到視頻裏有老師用PropTypes這個首次見面的組件,查看了幾篇文章,頓時恍然大悟。
官網對它的介紹Reusable Components PropTypes可以驗證用戶輸入數據的有效性,先上代碼
propTypes:{
name:React.PropTypes.string.isRequired
},
設定輸入數據爲字符串型,isRequired
表示不能爲空,另外,
getDefaultProps:function(){
return {
name:”kkll”
}
},
getDefaultProps
用於設定默認值,這裏設置name默認值爲kkll
看下運行結果
如果修改name的值得類型,會怎樣呢?
雖然結果會顯示,但是我們可以看到控制檯會出現一條警告:
參考文檔:推酷http://www.tuicool.com/articles/Ubm6zeE
react中文網:http://www.css88.com/react/docs/reusable-components.html