我想在某个事件中动态更改值

活动:

BackgroundGeolocation.on('location', (location) => {      
    currentDistance =  distance(previousLatitude,previousLongitude,latitude,longitude);

            this.setState({
                text: currentDistance                
            });                                   

    });
<Text>
   Moving : {this.state.text}
</Text>

有人知道如何更改文本或任何其他方法来实现吗?

推荐答案

下面是一个例子,它在点击文本值时使用状态来动态更改文本值.你可以设置任何你想要的活动.

import React, { Component } from 'react'
import {
   Text,
   View
} from 'react-native'

export default class reactApp extends Component {
   constructor() {
      super()
      this.state = {
         myText: 'My Original Text'
      }
   }
   updateText = () => {
      this.setState({myText: 'My Changed Text'})
   }
   render() {
      return (
         <View>
            <Text onPress = {this.updateText}>
               {this.state.myText}
            </Text>
         </View>
      );
   }
}

EDIT: Using React Hooks

import React, { useState } from 'react';
import { View, Text } from 'react-native';

const ReactApp = () => {
    const [myText, setMyText] = useState("My Original Text");
    return (
        <View>
            <Text onPress = {() => setMyText("My Changed Text")}>
                    {myText}
            </Text>
        </View>
    )
}

export default ReactApp;

React-native相关问答推荐

将屏幕标题更新为所选标签导航的标题

React Native - remount / reset / reload屏幕的最佳方式是什么?

react-native-snap-carousel 无法正确渲染

redux-observable 您在预期流的地方提供了 undefined

React Native 用于低互联网带宽的小型 apk APP应用

使用带有 react-native 的 WebView 设置用户代理

React-Native 按钮居中对齐

xcrun:错误:SDK "iphoneos" cannot be located

React Native 有全局作用scope域吗?如果没有,我该如何模仿它?

try 添加包时出现错误duplicate entry:com/google/android/gms/internal/zzble.class

是否需要使用 StyleSheet.create?

如何在自定义组件上使用 onPress?

React Native 元素,未生成 index.ios.js 或 index.android.js

如何在 React Native 的 MapView 中设置标记

如何在 react-native 中为在后台杀死的 android 应用程序保持状态

react-native:0.41 app.json

如何将图形 API 与 react-native-fbsdk 一起使用?

如何在react-native中更新数组状态?

React Native 多行TextInput,文本居中

将现有的 React Native 项目转换为 Expo