Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
Change a text value after pressing a button

Sorry, i'm new to react native. How do i do to make a button who change the value of a input text or just a text property? I'm trying to create a Text and put below a global variable (imc) with {} but it doesnt work to change the {imc} for the variable result, so, what i need to do now? My code and sorry for the mess of it ah:

import React, {useState} from 'react';
import { StyleSheet, View, Text, SafeAreaView, StatusBar, TextInput, TouchableOpacity, Image} from 'react-native';

export default function App() {
 
const [altura, setAltura] = useState('');
const [peso, setPeso] = useState('');
const [idade, setidade] = useState('');
const mudarResultado=()=>{};




var imc = peso / (altura*altura/10000)

 function Calcular(){
  

alert(`Altura: ${altura}cm \nPeso: ${peso}kg  \nSeu imc: ${imc.toFixed(2)} `);

if (imc < 18.5 ){
 alert("Você está muito magro");
} else if (imc > 18.5 && imc < 25){
  alert("Você está com um peso normal");
}  else if (imc >= 25 && imc < 30){
  alert("Você está com sobrepeso");
} else {
  alert("Você está na faixa de obesidade");
}


 }
 
  return(
  <SafeAreaView style={styles.container}>
    <StatusBar />

    


    <Text style={styles.titulo}>CALCULE SEU IMC</Text>

    {/* campo de texto altura */}
    <TextInput 
     style={styles.input}
    placeholder='Altura(cm)'
    keyboardType='numeric'
    value={altura}
    onChangeText={(altura)=>setAltura(altura)}
    />


{/* campo de texto peso */}

<TextInput 
     style={styles.input}
    placeholder='Peso (kg)'
    keyboardType='numeric'
    value={peso}
    onChangeText={(peso)=>setPeso(peso)}
    />

    {/* campo de texto idade */}

    <TextInput 
     style={styles.input}
    placeholder='Idade (anos)'
    keyboardType='numeric'
    value={idade}
    onChangeText={(idade)=>setidade(idade)}
    />

{/* Button */}
<TouchableOpacity style={styles.botao}onPress={Calcular}>


  <Text style={styles.btn_txt}>CALCULAR</Text>
  
</TouchableOpacity>

<Text style={styles.ideal}>Resultado IMC</Text>
<Text>   IMC: {imc} </Text>```
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I don't know if i understood your question, but, if you want to change a variable value, by pressing a button, you first have to put the variable in the "memory of the screen" that is, in the state, like you did in:

const [altura, setAltura] = useState('');

So you should do something like this

const [imc, setIMC] = useState(0);

And after, create a function that calculates the IMC, I recomend you always use arrow function because it prevents some bugs in the future.

    const CalculateIMC = () => {
         let imc = peso / (altura*altura/10000); // I used "let" instead of "var" because "let" only exists inside this function so it uses less memory. Think about largers aplications
         setIMC(imc); // Here you are setting the variable imc in the memory of yout screen
         // After this you can use your logic to determinates in what category the user is.
    }

After that you can delete this line

var imc = peso / (altura * altura / 10000)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!