Textinput value does not change - React Native

Asked

Viewed 28 times

0

So I’m taking these values in Asyncstorage and they come from my database, so far so good, I take the const’s and I play in them the values I pulled. but I need to be able to edit this by the text entries, and I can’t change the value of them.

import React, { useState, useEffect } from 'react'; 
import { View, Text, TextInput, SafeAreaView, StyleSheet, Image, TouchableOpacity, KeyboardAvoidingView } from 'react-native'; 
import {css} from '../../style/Css'
import MenuAreaRestrita from '../../assets/components/MenuAreaRestrita';
import AsyncStorage from '@react-native-async-storage/async-storage';


export default function Editar({navigation}){ 




const[paciente_nome, setPaciente_nome] = useState()
const[paciente_cpf, setPaciente_Cpf] = useState(null)

useEffect(()=> {
    async function getNo_completo(){
        let response = await AsyncStorage.getItem('pacienteData'); 
        let json = JSON.parse(response); 
        setPaciente_nome((json.no_completo).toString()) 
        setPaciente_Cpf(json.nu_cpf)
         
        
    } 

    getNo_completo(); 
    

}) 

return( 
    <View style={css.containerMenu}>  
    <MenuAreaRestrita title='Editar' navigation={navigation}/>
    <Text style={css.textCadastro}> Insira os dados do paciente:</Text>
    <TextInput style ={css.textInputCadastro}
                placeholder  ='Nome:'
                editable={true} 
                value={(paciente_nome).toString()}
                onChangeText ={text=>String(setPaciente_nome(text))}
            />
     <TextInput style ={css.textInputCadastro}
                placeholder  ='CPF:'
                editable
                value={formataCPF(String(paciente_cpf))}
                onChangeText ={text=>setPaciente_Cpf(text)}
            />
     
   
            
    </View>
)

}

  • What returns on this line? Let json = JSON.parse(Answer);

  • patient data. But anyway the error was in useEffect, I faltered and it turned out that this function was being called all the time, so I could n update

  • is even missed to put the array [] there in its dependencies. put an answer there can help other users.

  • Really, I’ll do it. VLW

1 answer

-1

So guys what happens is that my function was being called several times for so, always brought the old value.

The solution was as follows:

useEffect(()=> {
async function getNo_completo(){
    let response = await AsyncStorage.getItem('pacienteData'); 
    let json = JSON.parse(response); 
    setPaciente_nome((json.no_completo).toString()) 
    setPaciente_Cpf(json.nu_cpf)
     
    
} 

getNo_completo(); 


}, []) 

That is by an empty array at the end of the function, so it is called only once.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.