【发布时间】:2018-01-21 10:23:54
【问题描述】:
我一直在学习有关 udemy 的课程,但无论我做什么都会出现错误:
这是组件代码:
import React from 'react';
import { Component } from 'react';
import { connect } from 'react-redux';
import Chart from '../components/chart';
class WeatherList extends Component {
constructor(props) {
super(props);
}
renderWeather(cityData) {
...
}
render() {
return (
...
);
}
}
function mapStateToProps(state) {
return {
weather: state.weather
};
}
export default connect(mapStateToProps)(WeatherList);
这是我要导入的图表组件:
import React from 'react';
import { Sparklines, SparklinesLine, SparklinesReferenceLine } from 'react-sparklines';
import _ from 'lodash';
function average(data) {
return _.round(_.sum(data) / data.length);
}
const Chart = (props) => {
return (
<div>
<Sparklines width={80} height={80} data={thisprops.data}>
<SparklinesLine color={props.color} />
<SparklinesReferenceLine type="avg" />
</Sparklines>
<div>
{ average(props.data) } { props.units }
</div>
</div>
);
};
export default Chart;
但显然 React.Component 是未定义的,因此会引发错误。
【问题讨论】:
-
请尝试删除您的构造函数。根据 reactjs doc “如果您不初始化状态并且不绑定方法,则不需要为您的 React 组件实现构造函数。”
-
GabrielDiez 还是抛出同样的错误,我只是在前面添加了构造函数,看看它是否有任何作用。
-
是
Chart文件名真的小写chart? -
是的,错误与模块无关。
标签: javascript html reactjs