ReactJS: this.props.data.map is not a function

后端 未结 1 1967
迷失自我
迷失自我 2021-01-14 18:28

I am trying to get the data from a .json file and displaying the results through a view. Below is the scoreboard.json file.

{
\"subject\":\"scoreboard\",
\"c         


        
相关标签:
1条回答
  • 2021-01-14 19:27

    As pointed out by azium my data is an object and not an array like my results were expecting.

    To get the "game" array inside the "games" field I set the state as the following:

    this.setState({data: data.data.games.game});
    

    To get the other fields I simply created a new state and passed it through a prop i.e. modified_date:

    this.state.date = data.data.games.modified_date
    

    The warning: Each child in an array or iterator should have a unique "key" prop. was fixed by adding a "key" property inside the .map method:

    render: function() {
        var gameDate = this.props.data.map(function(data) {
            return (
                <h1 key={data.location} >{data.location} </h1>
            );
        });
    
    0 讨论(0)
提交回复
热议问题