【发布时间】:2020-10-04 16:48:43
【问题描述】:
我正在尝试使用以下react-native-echarts-wrapper,但在我的项目中,我的所有组件都是使用钩子制作的。因此,当我有一个状态变量改变其状态时,我想执行函数setOption(option),其中选项包含状态值。
问题是在文档中函数setOption() 是使用this.chart.setOption(option) 引用的。如果我尝试不使用this.,我会收到一条消息,其中chart 未定义,如果我只使用函数,我会收到一条消息说setOption is not a function。
那么有没有办法使用 RN Hooks 获取图表组件及其功能的引用?
这是我的代码:
const [radarChartData, setRadarChartData] = React.useState([])
const option = {
title: {
show:false,
text: 'Gráfico Radar'
},
tooltip: {},
legend: {
orient: 'horizontal',
position:'bottom',
left: 0,
bottom:0,
type:'scroll',
pageButtonPosition:'end',
data: chartColumnNameArray,
},
radar: {
radius:'70%',
name: {
textStyle: {
color: '#fff',
backgroundColor: '#999',
borderRadius: 3,
padding: [3, 5]
}
},
indicator: chartValueArray.map((item, index)=>{
return {name:chartColumnNameArray[index].toUpperCase().substring(0,5).concat('.'), max:maxValue}
})
},
series: [{
name: 'TEST',
type: 'radar',
data: radarChartData <------------------- //when this state changes I would setOptions and reload chart
}]
};
<View style={{display: visibleChart==="radarchart"?"flex":"none", width:'100%', height:'60%'}} >
<ECharts option={option} additionalCode={React.useEffect(()=>{this.chart.setOption(option)},[radarChartData])}/>
</View>
【问题讨论】:
标签: javascript react-native react-hooks echarts