【问题标题】:How to replace the 'this.' in the functional React Native with Hooks?如何替换“这个”。在带有 Hooks 的功能性 React Native 中?
【发布时间】: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


    【解决方案1】:

    您必须使用“useRef”挂钩才能访问图表,即使在提供的示例中,他们使用对图表的引用并更新它。 我制作了一个基本示例来更改背景颜色,这将使您了解如何将钩子与图表一起使用。只需设置 ref 并使用 chart.current 而不是 this.chart。

    import React, { Component } from 'react';
    import { StyleSheet, View, Button } from 'react-native';
    import { ECharts } from 'react-native-echarts-wrapper';
    
    export default function App() {
      const chart = React.useRef(null);
      const option = {
        xAxis: {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
        },
        yAxis: {
          type: 'value',
        },
        series: [
          {
            data: [820, 932, 901, 934, 1290, 1330, 1320],
            type: 'line',
          },
        ],
      };
    
      return (
        <View style={styles.chartContainer}>
          <Button
            title="Update"
            onPress={() => {
              if (chart) {
                chart.current.setBackgroundColor('rgba(0,255,0,0.3)');
              }
            }}
          />
          <ECharts
            ref={chart}
            option={option}
            backgroundColor="rgba(93, 169, 81, 0.4)"
          />
        </View>
      );
    }
    
    const styles = StyleSheet.create({
      chartContainer: {
        flex: 1,
      },
    });
    

    【讨论】:

    • 嗨,你能看看这个问题吗? stackoverflow.com/questions/66548327/…
    • 嗨@kd12345,你必须检查reactnavigation.org/docs/state-persistence
    • 我已经实现了类似的东西,当用户已经有一个帐户而不是导航到欢迎屏幕时,用户导航到密码屏幕。我的问题是例如。在应用程序的个人资料屏幕上说我,然后我导航到whatsapp,然后返回应用程序,当我在主屏幕而不是个人资料屏幕上返回应用程序时。
    • 你可能有一个异步存储来拥有登录状态,但导航状态不同,你可能需要检查我给你的链接
    • 感谢您深入了解并更新您
    猜你喜欢
    • 2020-11-19
    • 2021-05-10
    • 2021-10-02
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 2022-11-10
    相关资源
    最近更新 更多