我正在遵循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' –