Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

212
Visualizações
El texto no se actualiza en React Native

aplicación.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, }, });

Entonces, cuando presiono touchableOpacity, se supone que la variable de conteo agrega 0.25 a sí misma. Eso funciona bien, pero el texto

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

no se está actualizando. También me gustaría saber si la forma en que muestro el recuento de variables en <Text><Text/> es correcta. El texto solo muestra 5 No tengo experiencia previa con React native si me ayudaría, por favor.

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

React usa algunos ganchos para actualizar el DOM, no puede simplemente usar variables y esperar que actualice el DOM, en este caso necesita usar el gancho 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, }, });

Puede leer este artículo para comprender mejor reaccionar y cómo funciona.

about 4 years ago · Santiago Trujillo Relatório

0

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> ); }
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda