[英]this.setState() on react native not working with Text Inputs
I'm trying to print the user's info from firebase into placeholder of TextInput but when i try to set the info it isn't represented and node gives me a message:我正在尝试将 firebase 中的用户信息打印到 TextInput 的占位符中,但是当我尝试设置信息时它没有被表示并且节点给了我一条消息:
Error:错误:
Please update the following components: ProfileContent
LOG user logged
LOG -----O myemail.com
LOG -----O myname
LOG -----O mylastname
LOG -----O adriangc24
WARN Possible Unhandled Promise Rejection (id: 1):
TypeError: undefined is not an object (evaluating '_this2.setState')
My constructor:我的构造函数:
constructor(props) {
super(props);
this.state = {
name: "",
lastname: "",
displayname: "",
email: "",
};
}
state = {
name: "Nombre",
lastname: "Apellidos",
displayname: "Nombre de usuario",
email: "Correo electrónico",
};
TextInput example:文本输入示例:
<TextInput
autoCapitalize="sentences"
style={styles.userInput}
placeholder={this.state.lastname}
ref={"lastnamesInput"}
/>
My method for get the user's data from firebase:我从 firebase 获取用户数据的方法:
function getUserData() {
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
console.log("user logged");
var usuario = firebase.database().ref("/users/" + user.uid);
usuario.once("value").then((snapshot) => {
var usr = snapshot.val();
console.log("-----O " + usr.email);
console.log("-----O " + usr.name);
console.log("-----O " + usr.lastname);
console.log("-----O " + usr.displayname);
this.setState(
{
name: usr.name,
lastname: usr.lastname,
displayname: usr.displayname,
email: usr.email,
},
() => {
console.log("///////////////////////////////////////");
console.log("XXXXXX " + this.state.email);
}
);
});
} else {
console.log("user not logged");
}
});
}
It's the scope issue, due to function (user) {
you have lost the scope of this
, solution to this is use fat arrow function
. It's the scope issue, due to
function (user) {
you have lost the scope of this
, solution to this is use fat arrow function
.
Change this:改变这个:
firebase.auth().onAuthStateChanged(function (user) {
To至
firebase.auth().onAuthStateChanged((user) => {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.