简体   繁体   English

React Native 中的文本没有更新

[英]Text is not updating in React Native

app.js应用程序.js

import { StatusBar } from "expo-status-bar";
import {
  TouchableOpacity,
  Button,
  StyleSheet,
  Alert,
  SafeAreaView,
  Text,
} from "react-native";

export default function App() {
  let count = 5;
  let counts = [count];
  return (
    <SafeAreaView style={styles.container}>
      <Text style={styles.Text}>Earn Money</Text>
      <TouchableOpacity
        onPress={() => {
          count += 0.25;
          console.log(count);
        }}
        style={{
          height: 70,
          width: 130,
          backgroundColor: "#ff5c5c",
          alignSelf: "center",
          top: 25,
        }}
      >
        <Text
          style={{
            fontWeight: "900",
            alignSelf: "center",
            position: "relative",
            top: 25,
          }}
        >
          Earn 0.20$
        </Text>
      </TouchableOpacity>
      <Text style={styles.Balance}>{count}</Text>
      <StatusBar style="auto" />
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: Platform.OS === "android" ? 90 : 0,
    paddingLeft: Platform.OS === "android" ? 10 : 0,
  },
  Text: {
    justifyContent: "center",
    alignSelf: "center",
    color: "orange",
    fontSize: 25,
    fontWeight: "900",
  },
  Balance: {
    justifyContent: "center",
    alignSelf: "center",
    color: "orange",
    fontSize: 25,
    fontWeight: "900",
    top: 100,
  },
});

So when I press touchableOpacity the count variable is supposed to add 0.25 to itself, That is working fine but the text因此,当我按下 touchableOpacity 时,计数变量应该为自身添加 0.25,这工作正常,但文本

<Text style={styles.Balance}>{count}</Text>

is not updating.I would also want to know if the way I dispaly the variable count in <Text><Text/> is correct.没有更新。我还想知道我在<Text><Text/>中显示变量计数的方式是否正确。 THe text is just showing 5 I have no prior experience with React native if you would help pls do.文本仅显示 5 如果您愿意请帮忙,我之前没有使用 React native 的经验。

React uses some hooks for updating the DOM you can't just use variables and expect it to update the DOM, in this instance you need to use useState hook React 使用一些钩子来更新 DOM,你不能只使用变量并期望它来更新 DOM,在这种情况下你需要使用useState钩子

import { StatusBar } from "expo-status-bar";
import { useState} from "react";
import {
  TouchableOpacity,
  Button,
  StyleSheet,
  Alert,
  SafeAreaView,
  Text,
} from "react-native";

export default function App() {
  let [count, setCount] = useState(0);
  return (
    <SafeAreaView style={styles.container}>
      <Text style={styles.Text}>Earn Money</Text>
      <TouchableOpacity
        onPress={() => {
          setCount(c => c + 0.5)
        }}
        style={{
          height: 70,
          width: 130,
          backgroundColor: "#ff5c5c",
          alignSelf: "center",
          top: 25,
        }}
      >
        <Text
          style={{
            fontWeight: "900",
            alignSelf: "center",
            position: "relative",
            top: 25,
          }}
        >
          Earn 0.20$
        </Text>
      </TouchableOpacity>
      <Text style={styles.Balance}>{count}</Text>
      <StatusBar style="auto" />
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: Platform.OS === "android" ? 90 : 0,
    paddingLeft: Platform.OS === "android" ? 10 : 0,
  },
  Text: {
    justifyContent: "center",
    alignSelf: "center",
    color: "orange",
    fontSize: 25,
    fontWeight: "900",
  },
  Balance: {
    justifyContent: "center",
    alignSelf: "center",
    color: "orange",
    fontSize: 25,
    fontWeight: "900",
    top: 100,
  },
});

You can read this article to better understand react and how it works.您可以阅读这篇文章以更好地了解 React 及其工作原理。

import { useState} from "react";
const [count,setCount]=useState(5) // 5 is default value
const [update,setUpdate]=useState(0) // add this
export default function App() {
  let count = 5;
  let counts = [count];
  return (
    <SafeAreaView style={styles.container}>
      <Text style={styles.Text}>Earn Money</Text>
      <TouchableOpacity
        onPress={() => {
          count += 0.25;
          setCount(count)       // add this
          setUpdate(update+1)   // add this
           
          console.log(count);
        }}
        style={{
          height: 70,
          width: 130,
          backgroundColor: "#ff5c5c",
          alignSelf: "center",
          top: 25,
        }}
      >
        <Text
          style={{
            fontWeight: "900",
            alignSelf: "center",
            position: "relative",
            top: 25,
          }}
        >
          Earn 0.20$
        </Text>
      </TouchableOpacity>
      <Text style={styles.Balance}>{count}</Text>
      <StatusBar style="auto" />
    </SafeAreaView>
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM