繁体   English   中英

反应组件无法正确加载

[英]React Component not loading correctly

我有一个简单的React应用连接到Firebase Firestore。 这是很原始的,到目前为止应该只加载列表中people并显示出来。

这是App.js和Person.js组件,它们非常简单,我遇到一个问题,即即使它是从Firebase正确获取信息,也无法加载Person组件。 第一个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;

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

您所遇到的问题很可能是渲染已完成,除非调用setState,否则不会重新渲染。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM