【发布时间】:2021-06-07 09:55:30
【问题描述】:
我是新来的反应并且在从组件中的firebase获取数据后无法设置状态确实挂载功能,不断收到错误联系未在组件内部定义并挂载。
constructor(props) {
super(props);
this.state = {
contacts: [
{ name: 'sachin', number: '445343' },
{ name: 'nihil', number: '33335555' },
{ name: 'chicken', number: '434355' },
]
}
this.componentDidMount = this.componentDidMount.bind(this);
}
componentDidMount = () => {
let currentComponent = this;
fire.auth().onAuthStateChanged(function (user) {
if (user) {
console.log(user.uid)
var uid = user.uid;
db.collection(uid)
.get()
.then((querySnapshot) => {
const contacts = querySnapshot.forEach((doc) => {
const data = doc.data()
contacts.push(data)
})
console.log(contacts)
this.setState({contacts});
});
} else {
// No user is signed in.
}
});
}
【问题讨论】:
标签: javascript reactjs firebase jsx