Sam Fisher
Sam Fisher

Reputation: 848

Why mistake appears during render?

I'm using react-highcharts in my project. I want to generat chart-component dynamicaly and change their number according to user's choice. I wrote component and function which gets data and makes array of highcharts component and returns them:

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


}

During render I get mistake:

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.

What can be a reason? And how can I solve it.

Upvotes: 3

Views: 112

Answers (1)

Sagiv b.g
Sagiv b.g

Reputation: 31024

You forgot to call this.getChartItemsList():

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

By the way you can do the condition with this syntax:

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

Upvotes: 1

Related Questions