简体   繁体   中英

JSX element type 'Line' is not a constructor function for JSX elements

I am trying to use chart.js with Typescript to build an application in React, however when attempting to render a Line chart I get the error described above on this line:

<Line data={data}/>.

Along with this I also get the message "Type 'ChartComponent' is missing the following properties from type 'ElementClass': render, context, setState, forceUpdate, and 3 more."

Furthermore, when I use yarn start to run the application, the graph will render, but only for a split second before crashing with the error message.

I am fairly new to Typescript so I am confused as to what this error means. Any help would be appreciated.

import React, {Component} from 'react';
import Line from "react-chartjs-2"

class LineTest extends React.Component{

    render() {
        const data = {
          labels: [
            '10/04/2018', '10/05/2018', 
            '10/06/2018', '10/07/2018', 
            '10/08/2018', '10/09/2018', 
            '10/10/2018', '10/11/2018', 
            '10/12/2018', '10/13/2018', 
            '10/14/2018', '10/15/2018'
          ],
          datasets: [
            {
              label: 'Temperature',
              data: [22,19,27,23,22,24,17,25,23,24,20,19],
              fill: false,          
              borderColor: 'green'  
            }
          ]
        }

        return (
          <div className="App">
            <div>
              <Line data={data}/>
            </div>
          </div>
        );
      }
}

export default LineTest

The Line component is exported by react-chartjs-2 as a named export. You need to import it by its name:

import {Line} from 'react-chartjs-2';

Live Example:

编辑aughing-cdn-3qp5q

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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