ReactJs-未捕获类型错误:无法读取属性';字符串';未定义的
我的2个文件: 应用程序:ReactJs-未捕获类型错误:无法读取属性';字符串';未定义的,reactjs,Reactjs,我的2个文件: 应用程序: import React, { Component } from 'react'; import { NICE, SUPER_NICE } from './colors'; import Counter from './counter'; export class App extends Component { render() { return ( <div> <Counter increment={ 1
import React, { Component } from 'react';
import { NICE, SUPER_NICE } from './colors';
import Counter from './counter';
export class App extends Component {
render() {
return (
<div>
<Counter increment={ 1 } color={ NICE } />
<Counter increment={ 5 } color={ SUPER_NICE } />
</div>
);
}
}
import React, { Component } from 'react';
class Counter extends Component {
constructor(props) {
super(props);
this.state = { counter: 0 };
this.interval = setInterval(() => this.tick(), 1000);
}
tick() {
this.setState({
counter: this.state.counter + this.props.increment
});
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
return (
<h1 style={ { color: this.props.color } }>
Counter ( { this.props.increment } ): { this.state.counter }
</h1>
);
}
}
Counter.defaultProps = {
increment: 0,
color: null
};
Counter.propTypes = {
increment: React.PropTypes.number.isRequired,
color: React.propTypes.string.isRequired
};
module.exports = Counter;
"react": "^0.14.0-rc1",
"eslint": "^1.3.1",
"eslint-config-airbnb": "^0.1.0",
"eslint-loader": "^1.0.0",
"eslint-plugin-react": "^3.0.0"
我需要声明propTypes以满足eslint,但我收到一个运行错误:
“未捕获类型错误:无法读取未定义的属性“字符串”。
有人看到我的错误了吗
非常感谢 在计数器propTypes上,“color”React.propTypes应大写
Counter.propTypes = {
increment: React.PropTypes.number.isRequired,
color: React.PropTypes.string.isRequired
};
我在
React.PropTypes
中遇到了类似的问题,当我使用React-16时,我发现React.PropTypes
不受支持。
因此,您需要使用安装“^15.6.0”“道具类型”:
npm install prop-types --save
然后按如下所示导入它
import PropType from 'prop-types'
导入时我犯了一个错误,而不是导入道具类型
,这就是我的问题所在
作为答案发布,以便其他人了解更新的软件包以及如何安装等。我觉得自己像个傻瓜。谢谢谢谢你的回答。它为我解决了问题!只有一种类型需要修复!从“道具类型”导入道具类型-->从“道具类型”导入道具类型。此外,我还将-g参数用于npm安装-->npm安装-g道具类型--save