简体   繁体   中英

Why am I getting an unexpected token error in React when using createRef()

The error is pointing towards the space right before the = in the class.

This is the error C:\Users\crism\dream-in-green\src\components\myLineGraph.js:5:13: C:/Users/crism/dream-in-green/src/components/myLineGraph.js: Unexpected token (5:13)

It also says that the chartRef in the first line of code is never being used even though it's being called in the line below the class.

Any ideas?

import React, { Component, chartRef } from 'react';
import Chart from "chart.js";

export default class LineGraph extends Component {
    chartRef = React.createRef();

}

I think chartRef should be declared as const instead of importing it from react library

This is the correct way to do it: this.chartRef = React.createRef(); You don't need to declare it at the top of the file

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