引用获取实例

子组件

import * as echarts from 'echarts';
import React,{forwardRef} from "react";
import ReactECharts from 'echarts-for-react';
// var chartDom = document.getElementById('main');
// var myChart = echarts.init(chartDom);
// var option;

const Line = (props,ref) => {
    const {data} = props
    const getOption = () => {
        let option = {
            xAxis: {
                type: 'category',
                data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
            },
            tooltip: {
                trigger: 'axis'
            },
            yAxis: {
                type: 'value'
            },
            series: [{
                data: data,
                type: 'line'
            }]
        };
        return option
    }

    return (
        <>
            
        
    )

}

export default  forwardRef(Line);

父组件

  const ref = createRef();
  useEffect(() => {
    var echarts = require('echarts')
    const { current } = ref;
    const echartsInstanceone = current.getEchartsInstance();
    echartsInstanceone.group = 'group1';
    console.log(echartsInstanceone)
    echarts.connect('group1')
  }, [])

render中
Line data={item} ref={ref} >

你可能感兴趣的:(引用获取实例)