0
How to Fill my Textinputs through my database so that the User can edit them and insert them again (replacing the old ones) for example:(take my email and password I have in BD and put in these Textsinputs to then edit and how to send again to BD), I am beginner and because of this I do not know how I could do correctly...
import React, {Component} from 'react';
import {View, Text, StyleSheet, Button, TextInput} from 'react-native';
import firebase from 'firebase';
export default class TelaLogin extends Component{
constructor(props) {
super(props);
this.state = {
cidadeINput:'',
emailInput:'',
estadoInput:'',
nomeINput:'',
senhaInput:'',
usuarioInput:'',
};
let config = {
apiKey: "AIzaSyBEAn6v3qKO8m7jEsO7JlpTAnsCvKSoevo",
authDomain: "adote-ja.firebaseapp.com",
databaseURL: "https://adote-ja.firebaseio.com/",
projectId: "adote-ja",
storageBucket: "adote-ja.appspot.com",
messagingSenderId: "1081270881358"
};
firebase.initializeApp(config);
this.inserirUsuario = this.inserirUsuario.bind(this);
}
inserirUsuario() {
if( this.state.usuarioInput.length > 0 ){
let usuarios = firebase.database().ref('usuarios');
let chave = usuarios.push().key;
usuarios.child(chave).set({
usuario:this.state.usuarioInput,
senha:this.state.senhaInput
});
alert("senha inserida")
}
}
render(){
return(
<View style = {styles.container}>
<TextInput style={styles.input} placeholder="Usuário" onChangeText={(usuarioInput)=>this.setState({usuarioInput})} />
<TextInput style={styles.input} placeholder="Senha" onChangeText={(senhaInput)=>this.setState({senhaInput})} />
<Button title="inserir usuário" onPress={this.inserirUsuario} />
</View>
);
}
}
const styles = StyleSheet.create({
container:{
flex: 1,
marginTop: 20,
padding: 20,
},
input:{
height: 40,
borderWidth: 1,
borderColor: '#FF0000',
},
});
Friend, it was not quite this problem that I have, I need to bring the textsinputs filled by data from my BD so that the user can edit it dps and click update
– Edu_gamer _programer
Yes, to fill in the data coming from the database, the component needs to be mounted first and only after that you arrow the state with the data coming from the component. That’s why you use the
ComponentDidMount
to do this. place your function to fetch the data inside the compomentDidMout.– Carlos Querioz
friend you could give me an example with the code for me to see/
– Edu_gamer _programer
write an example in code as it would be
– Edu_gamer _programer
I changed my first answer by adding the IdMount component. This is the answer, you just have to see if the method you use to call Firebase is right and if the data that is returned is the same. You may need to insert an Async/Await in the method.
– Carlos Querioz