渲染期间为什么出现错误?

问题描述 投票:3回答:1

我在我的项目中使用react-highcharts。我想动态生成图表组件并根据用户的选择更改其编号。我编写了组件和函数,它获取数据并生成highcharts组件数组并返回它们:

export default class ContentChartItem extends Component {

    constructor(props) {
        super(props);
        this.state = {
        }

        this.config = {}

    render() {
        let { process_data } = this.props;
        let config = this.config;

        return (
            <div className="column">
                {typeof process_data == "undefined" ? " "
                    :
                    <div className="ui segment">
                        {this.getChartItemsList}
                        <div className="ui button tiny fluid sidenav4">
                            Info
                        </div>
                    </div>
                }
            </div>
        )
    }

    getChartItemsList = () => {
        let config = this.config;
        let { process_data } = this.props;
        let chartContainers = [];
        for ( let i=0; i<process_data.length; i++ ) {
            chartContainers.push( <ReactHighcharts config = {config}> </ReactHighcharts> )
        }

        return chartContainers;
    };


}

在渲染过程中我得到错误:

Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.

可能是什么原因?我该怎么解决呢。

javascript reactjs highcharts react-highcharts
1个回答
1
投票

你忘了打电话给this.getChartItemsList()

{typeof process_data == "undefined" ? " "
                    :
                    <div className="ui segment">
                        {this.getChartItemsList()}
                        <div className="ui button tiny fluid sidenav4">
                            Info
                        </div>
                    </div>
                }

顺便说一下,您可以使用以下语法执行此操作:

{
  process_data && (
  <div className="ui segment">
    {this.getChartItemsList()}
    <div className="ui button tiny fluid sidenav4">
      Info
    </div>
  </div>
)}
© www.soinside.com 2019 - 2024. All rights reserved.