在學習react的時候,測試props驗證的時候,一直卡在的propTypes使用出錯
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
...
// props驗證
var title = "菜鳥驛站";
class MyTitle extends React.Component{
render(){
return(
<h1>Hello,{this.props.title}</h1>
);
}
}
MyTitle.PropTypes = {
title: PropTypes.string
};
ReactDOM.render(
<MyTitle title={title} />,
document.getElementById('example')
)
怎麼改都報以下錯誤:
Failed to compile.
./src/index.js
Line 270:9: Typo in static class property declaration react/no-typos
Search for the keywords to learn more about each error.
百思不得其解,因爲都是自學。第一次,找到一個全英回答,看都沒看就關了。又改了一陣子,還是改不了,又去看了那篇全英的問答,才發現,原來就是我粗心了。就是單純大小寫的問題。應該是propTypes而不是PropTypes。
import React from 'react';
import ReactDOM from 'react-dom';
import propTypes from 'prop-types';
...
// props驗證
var title = "菜鳥驛站";
class MyTitle extends React.Component{
render(){
return(
<h1>Hello,{this.props.title}</h1>
);
}
}
MyTitle.propTypes = {
title: propTypes.string
};
ReactDOM.render(
<MyTitle title={title} />,
document.getElementById('example')
)
搞定。