【发布时间】:2018-06-05 18:10:10
【问题描述】:
我有一个连接到 Firebase Firestore 的简单 React 应用程序。到目前为止它非常原始,它应该只加载people 的列表并显示它们。
这是 App.js 和 Person.js 组件,它们非常简单,我遇到了一个问题,即它没有加载 Person 组件,即使它从 firebase 正确获取信息。第一个 console.log() 工作正常,但似乎其余的从未被调用。谁能发现这个问题?没有任何错误消息或警告,它只是没有越线{this.state.people.map(person => ...
//Person.jsx
import React, { Component } from 'react';
import './Person.css';
import PropTypes from 'prop-types';
class Person extends Component {
constructor(props) {
super(props);
console.log('from person constructor: props are:' + props);
this.fistName = props.firstName;
this.lastName = props.lastName;
this.personId = props.personId;
this.createdDate = props.createdDate;
}
render(props) {
console.log('from person renderer: props are:' + props);
return (
<div className="person-wrapper">
<p className="personId"> Person ID : {this.personId}</p>
<p className="firstName"> First Name : {this.firstName}</p>
<p className="lastName"> Last Name : {this.lastName}</p>
</div>
);
}
}
Person.propTypes = {
firstName: PropTypes.string,
lastName: PropTypes.string,
personId: PropTypes.string,
createdDate: PropTypes.string
};
export default Person;
第二个文件:
//App.js
import React, { Component } from 'react';
import './App.css';
import Person from './Person/Person';
import { DB_CONFIG } from './Config/config';
import firebase from 'firebase';
class App extends Component {
constructor(props) {
super(props);
this.addPerson = this.addPerson.bind(this);
if (!firebase.apps.length) {
firebase.initializeApp(DB_CONFIG);
}
this.db = firebase.firestore();
const settings = { timestampsInSnapshots: true };
this.db.settings(settings);
this.state = {
people: []
};
}
componentWillMount() {
var allPeople = [];
this.db.collection('people').onSnapshot(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
allPeople.push(doc.data());
});
});
this.setState({
people: allPeople
});
console.log(allPeople);
}
addPerson(person) {
//this.db.push().set({firstName:person,lastName:person});
this.db
.collection('people')
.doc(person.personId)
.set({
firstName: person.firstName,
lastName: person.lastName
})
.then(function() {
console.log('Successfully added person: ' + person.firstName);
const allPeople = this.state.people;
var currentSnap = snap => {
allPeople.push({
personId: snap.key,
firstName: snap.val().firstName,
lastName: snap.val().lastName
});
this.setState({
people: allPeople
});
};
})
.catch(function(error) {
console.error('Error writing document :', error);
});
}
render() {
return (
<div className="peopleWrapper">
<div className="peopleHeader">
<h1>EG Resources:</h1>
</div>
<div className="peopleBody">
{console.log(this.state.people)}
{this.state.people.map(person => {
console.log(person);
return (
<Person
firstName={person.firstName}
lastName={person.lastName}
key={person.personId}
/>
);
})}
</div>
<div className="PeopleFooter" />
</div>
);
}
}
export default App;
【问题讨论】:
-
可能当你在请求后立即调用 setState 时,setState 发生在请求完成之前,并且你没有向 state 添加新人,尝试使用 then 并使用 setState
-
但是如果
this.state.people没有被填充,我不会看到控制台日志被正确填充,对吧? `{console.log(this.state.people)}行工作正常并打印出人员列表
标签: javascript arrays reactjs google-cloud-firestore