【发布时间】:2018-10-02 04:38:24
【问题描述】:
我的渲染函数中有一个 react-chartjs-2 元素,我从 React.createRef() 分配了一个引用,但是
- 引用为空
-
我的 ref 上的当前属性为 null
import React, { Component } from 'react'; const LineChart = require('react-chartjs-2').Line; class App extends Component { constructor(props) { super(props); this.chartRef = React.createRef(); this.chartObject = null; } componentDidMount() { this.chartObject = this.chartRef.current.getContext('2d'); this.chartObject.update(); } render() { return ( <div className = "App"> <LineChart data = {} width = {600} height = {250} ref = {this.chartRef}/> </div> ); } }
【问题讨论】:
-
你在使用 React 16.3 吗?
-
是的,我使用的是 16.3 (16.3.2)