React Component未正确加载

时间:2018-06-05 18:10:10

标签: javascript arrays reactjs google-cloud-firestore

我有一个简单的React应用程序连接到firebase firestore。它到目前为止非常原始,它应该只加载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;

1 个答案:

答案 0 :(得分:-1)

尝试添加名为isFetchComplete的状态变量,当从firebase存储中获取数据时,该变量将设置为true,否则为false。 然后在你的组件渲染中做if(!isFetchComplete) { /* render ..loading */ } else { /* render the component with the data */

你的情况下的问题可能是渲染完成,除非调用setState,否则不会重新渲染。