简体   繁体   English

React ES6组件不会重新发送

[英]React ES6 components are not re-rending

I have the following code. 我有以下代码。 Please notice the two console.log s. 请注意两个console.log I am calling the _getChartData() function from within another child of the <ChartTitle> component. 我从<ChartTitle>组件的另一个子_getChartData()调用_getChartData()函数。

The issue is that when _getChartData() is called a second time, it doesn't re-re-render the child components of the code below. 问题是当第二次调用_getChartData()时,它不会重新呈现下面代码的子组件。 I am expecting it to do so because of the setState in there. 我期待它这样做,因为那里有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 is what I get in my console: 这是我在我的控制台中得到的:

我的控制台记录

I think this is your problem 我想这是你的问题

this.setState = ({

setState is a function, you need to call it not assign it, ie: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM