简体   繁体   English

我必须单击两次显示按钮才能将数据呈现到屏幕上?

[英]I have to click the display button twice to render the data to the screen?

In this react component, I want to render the data to the screen when the 'Display' button is clicked by the user, but the button has to be pressed twice for the data to display. 在此React组件中,我想在用户单击“显示”按钮时将数据呈现到屏幕上,但是必须按下两次按钮才能显示数据。 Any Suggestions? 有什么建议么?

constructor(props) {
    super(props);

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

}

Here's my displayData function: 这是我的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
     });
}

And my render() just maps each person in the state with their properties: 而且我的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>

    );
}

Try this, hope this will work 试试这个,希望这能起作用

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 : []
        });
    });
}

dbRef.on is asynchronous code, move the this.setState code inside the callback of dbRef.on . dbRef.on是异步代码,移动this.setState代码的回调内部dbRef.on

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

The issue with your original code was execution of this.setState before completion of async dbRef.on 与原代码的问题是执行this.setState异步完成前dbRef.on

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

相关问题 我必须单击按钮两次才能使状态在 React 中正确呈现 - I have to click the button twice to get states to render correctly in React 我必须单击按钮两次才能将数据发布到 api 以做出反应 - i have to click on the button twice to post data to api in react 我需要在按钮上单击两次以显示组件 - I need to click twice on ,the button to display the component 我必须点击两次按钮才能提交 - I have to click button twice for submit 我必须单击两次按钮才能添加评论 - I have to click on the button twice to add a comment 为什么我必须单击按钮两次才能加载图像? - Why do I have to click the button twice to have the image loaded? 如何在本机反应中呈现 FlatList 中的数据,我可以控制台记录它但在屏幕上显示它时出现问题 - How to render data in FlatList in react native, I can Console log it but have a problem to display it on Screen 仅当我单击“提交”按钮两次时才会显示表单结果 - Form results display only if I click the “Submit” button twice 为什么必须两次单击此输入按钮才能调用函数? - Why do I have to click this input button twice to call a function? 我必须单击两次才能激活任何按钮动作(Ionic / Angular) - I have to click twice to activate any button action (Ionic/Angular)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM