简体   繁体   English

"为什么我的数据没有从 this.state 呈现?"

[英]Why is my data not rendering from this.state?

I've been trying to get an array to come over into react and then display it on the browser.我一直在尝试让一个数组过来做出反应,然后在浏览器上显示它。 The data is coming over, but for some reason I cannot display it.数据即将到来,但由于某种原因我无法显示它。

ReactJs:反应:

import React, { Component } from "react";

export default class RegisterPage extends Component {
    constructor(props){
        super(props);
        this.state = {
            
        };

        this.getPlayers();
    }

    getPlayers() {
        fetch('/draft/get-players')
        .then((response) => response.json())
        .then((data) => {
            this.setState({
                players: data,
            });
        });
    }

    render(){
        return (<h1>{this.state.players[0].name}</h1>)
    }
}

I initially was trying to .map this into multiple lines of HTML, but through troubleshooting learned that I'm not even able to get a single element out of the array.我最初试图将其映射到多行 HTML 中,但通过故障排除了解到我什至无法从数组中获取单个元素。

JSON from /draft/get-players:来自 /draft/get-players 的 JSON:

[
    {
        "id": 1,
        "name": "Aidan Hutchinson",
        "position": "EDGE",
        "EstimatedRound": 1,
        "college": "Michigan",
        "age": "SR",
        "TakenRound": null,
        "TakenPick": null
    },
    {
        "id": 2,
        "name": "Aidan Hutchinson",
        "position": "EDGE",
        "EstimatedRound": 1,
        "college": "Michigan",
        "age": "SR",
        "TakenRound": null,
        "TakenPick": null
    },
    {
        "id": 3,
        "name": "Kayvon Thidobeaux",
        "position": "EDGE",
        "EstimatedRound": 1,
        "college": "Oregon",
        "age": "SOPH",
        "TakenRound": null,
        "TakenPick": null
    },
    {
        "id": 4,
        "name": "Kyle Hamilton",
        "position": "S",
        "EstimatedRound": 1,
        "college": "Notre Dame",
        "age": "JR",
        "TakenRound": null,
        "TakenPick": null
    },
    {
        "id": 5,
        "name": "Ikem Ekwonu",
        "position": "OL",
        "EstimatedRound": 1,
        "college": "NC State",
        "age": "SOPH",
        "TakenRound": null,
        "TakenPick": null
    }
]

\/\/you have not declared the array players just initialize the array first it will work \/\/你还没有声明数组 player 只是先初始化数组它会起作用

 this.state = {
              players:[]
           }

I figured out the issue.我弄清楚了这个问题。

The page was rendering before the data was fetched, so when the page rendered it threw an error because 'this.state.players' was empty.该页面在获取数据之前正在呈现,因此当页面呈现时它会抛出错误,因为“this.state.players”是空的。

I fixed this by creating the element with the intial render knowing it would be empty, but when the state changes it updates to element.我通过使用初始渲染创建元素来解决此问题,知道它将是空的,但是当状态更改时它会更新为元素。 The element contains the JSX which will then render with my information.该元素包含 JSX,然后它将使用我的信息呈现。

Code:代码:

import React, { Component } from "react";

export default class RegisterPage extends Component {
    constructor(props){
        super(props);
        this.state = {
            players:[],
         }

        this.getPlayers();
    }


    getPlayers() {
        fetch('/draft/get-players')
        .then((response) => response.json())
        .then((data) => {
            this.setState({
                players: data,
            });
        });
    }

    render(){
        let players = this.state.players.map((player)=>{
            return(
                <div>
                    {player.name}
                </div>
            );
        }
        );
        return (
            <div>
                {players}
            </div>)
    }
}

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

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