我正在遵循React教程here。在第四課中,我創建了一個App.propTypes
部分。當我運行代碼時,React給我一個錯誤說TypeError: Cannot read property 'string' of undefined
當我打開我的控制檯時,錯誤說React.PropTypes is deprecated since React 15.5.0, use the npm module prop-types instead
。然後我繼續安裝npm包prop-types並將其導入到我的代碼中,但我仍然以相同的錯誤結束。我將在下面包含我的代碼。React.PropTypes已棄用,且已安裝道具類型包
我正在使用節點版本v8.5.0。也許我應該嘗試找出教程使用的節點版本,以便我的React版本匹配,但我甚至不知道我是否可以找到它,我希望教程能夠指定這些類型的東西,看起來像這樣教程是2歲,這可能是爲什麼我有這種差異。
的src/app.js
import React from 'react';
import PropTypes from 'prop-types';
class App extends React.Component{
render(){
let txt = this.props.txt
return (
<div>
<h1>{txt}</h1>
<b>bold</b>
</div>
)
}
}
App.propTypes = {
txt: React.PropTypes.string,
cat: React.PropTypes.number.isRequired
}
export default App;
/src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<App cat={5} txt="this is the prop value" />,
document.getElementById('root')
);
'txt:React.PropTypes.string' =>'txt:PropTypes.string' –