[英]React ES6 components are not re-rending
我有以下代码。 请注意两个console.log
。 我从<ChartTitle>
组件的另一个子_getChartData()
调用_getChartData()
函数。
问题是当第二次调用_getChartData()
时,它不会重新呈现下面代码的子组件。 我期待它这样做,因为那里有setState
。
export default class PerformanceChart extends React.Component {
constructor(props) {
super(props);
this.state = { chart_data: [] };
this._getChartData();
}
componentDidMount() {
this._getChartData();
}
_getChartData(d = 30) {
console.log('running setState for chart data');
// AJAX GOES HERE
this.setState = ({
chart_data : [
['x', '2016-01-01', '2016-03-01', '2016-04-01', '2016-05-01', '2016-06-01', '2016-07-01', '2016-08-01'],
['Clicks', Math.random(), 200, 100, 400, 150, 250, 125, 600],
['Orders Reported', 300, 250, 100, 400, 150, 250, 40, 300],
['totals', [1050, 5042]]
]
});
};
render() {
console.log('re-rendering!');
return (
<div>
<ChartTitle chart_data={this.state.chart_data} getChartData={this._getChartData.bind(this)} />
<Chart chart_data={this.state.chart_data} />
</div>
);
}
};
这是我在我的控制台中得到的:
我想这是你的问题
this.setState = ({
setState
是一个函数,你需要调用它而不是赋值它,即:
this.setState({
chart_data : [
['x', '2016-01-01', '2016-03-01', '2016-04-01', '2016-05-01', '2016-06-01', '2016-07-01', '2016-08-01'],
['Clicks', Math.random(), 200, 100, 400, 150, 250, 125, 600],
['Orders Reported', 300, 250, 100, 400, 150, 250, 40, 300],
['totals', [1050, 5042]]
]
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.