【问题标题】:I have to click the display button twice to render the data to the screen?我必须单击显示按钮两次才能将数据呈现到屏幕上?
【发布时间】:2018-09-14 11:38:32
【问题描述】:

在这个 react 组件中,我想在用户单击“显示”按钮时将数据呈现到屏幕上,但是必须按下该按钮两次才能显示数据。

constructor(props) {
    super(props);

    this.state = {
        people: [] // start with empty array of people
    }
    
}

这是我的 displayData 函数:

displayData = () => {
    const db = fire.database(); // database instance
    const dbRef = db.ref(); // database reference
    var newPeopleArray = []; // new array to push everyone from database into

    dbRef.on('value', snapshot => {

        snapshot.forEach(childSnapshot => { // for every child in the data snapshot
            var key = childSnapshot.key; // in this case returns first name
            var childData = childSnapshot.val();

            var newPerson = { // create new person object to push into the array
                name: key,
                age: childData.Age,
                lastName: childData.LastName
            };

            // console.log(newPerson);
            newPeopleArray.push(newPerson);
      
        });

    });

    // set state to the new array of people from the database
     this.setState({ 
         people: newPeopleArray
     });
}

而我的 render() 只是将状态中的每个人映射到他们的属性:

render() {     

    const stateToRender = this.state.people.map( person => // maps each person in the state to a row in the table
        <div key={person.name}>
            {person.name}
            {person.lastName}
            {p.age}
        </div>
       
    );

    return (
        <div className="container">
            
            <h1>Home Page</h1>
            <br />
            <button className="btn btn-danger" onClick={this.displayData}>Display</button>   
            {stateToRender}
 
                         
        </div>
        
    );
}

【问题讨论】:

    标签: javascript reactjs firebase


    【解决方案1】:
    displayData = () => {
        const db = fire.database(); // database instance
        const dbRef = db.ref(); // database reference
        var newPeopleArray = []; // new array to push everyone from database into
    
        dbRef.on('value', snapshot => {
            newPeopleArray = snapshot.map(childSnapshot => { // for every child in the data snapshot
                var key = childSnapshot.key; // in this case returns first name
                var childData = childSnapshot.val();
                var newPerson = { // create new person object to push into the array
                    name: key,
                    age: childData.Age,
                    lastName: childData.LastName
                };
                return newPerson;
            });
            // set state to the new array of people from the database
            this.setState({
                ...this.state,
                people: newPeopleArray ? newPeopleArray : []
            });
        });
    }
    

    【讨论】:

      【解决方案2】:

      dbRef.on是异步代码,把this.setState代码移到dbRef.on的回调里面。

      // set state to the new array of people from the database
       this.setState({ 
           people: newPeopleArray
       });
      

      您的原始代码的问题是在完成异步 dbRef.on 之前执行 this.setState

      【讨论】:

        猜你喜欢
        • 2015-07-02
        • 2020-06-29
        • 1970-01-01
        • 2019-01-18
        • 2013-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-01
        相关资源
        最近更新 更多