gpt4 book ai didi

javascript - React ES6 组件不会重新渲染

转载 作者:行者123 更新时间:2023-11-30 11:48:08 25 4
gpt4 key购买 nike

我有以下代码。请注意两个console.log秒。我调用 _getChartData()来自 <ChartTitle> 的另一个子项的函数成分。

问题是当_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>

);
}

};

这是我在控制台中得到的:

My console logs

最佳答案

我认为这是你的问题

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]]
]
});

关于javascript - React ES6 组件不会重新渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40250201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com