简体   繁体   English

我如何在React Native中解决uniqe关键道具问题

[英]How can i solve uniqe key prop problem in React native

I have React Native project and when i try to build my phone, i'm getting this error message. 我有React Native项目,当我尝试构建手机时,出现此错误消息。 Warning: Each child in an array or iterator should have a unique "key" prop. 警告:数组或迭代器中的每个子代都应具有唯一的“键”道具。 But it works on computer emulator. 但是它可以在计算机模拟器上运行。

模拟器 电话

Here is json data: http://hazir.net/api/match/today 这是json数据: http : //hazir.net/api/match/today

and here is my codes, 这是我的代码,

import React from 'react';
import { FlatList, ActivityIndicator, Text, View  } from 'react-native';

export default class Matches extends React.Component {

  constructor(props){
    super(props);
    this.state ={ isLoading: true}
  }

  componentDidMount(){
    return fetch('http://hazir.net/api/match/today')
      .then((response) => response.json())
      .then((responseJson) => {

        this.setState({
          isLoading: false,
          dataSource: responseJson,
        }, function(){

        });

      })
      .catch((error) =>{
        console.error(error);
      });
  }



  render(){

    if(this.state.isLoading){
      return(
        <View style={{flex: 1, padding: 20}}>
          <ActivityIndicator/>
        </View>
      )
    }

    return(
      <View style={{flex: 1, paddingTop:20}}>
        <FlatList
          data={this.state.dataSource}
          renderItem={({item}) => <Text>{item.matchId}, {item.matchDate}</Text>}
          keyExtractor={({id}, index) => id}
        />
      </View>
    );
  }
}

The easiest solution would be to update your keyExtractor as it appears to not be working. 最简单的解决方案是更新keyExtractor,因为它似乎不起作用。 It doesn't look like your data has an id property, however it does have a matchId property. 它看起来不像您的数据具有id属性,但确实具有matchId属性。 This means you could do something like the following. 这意味着您可以执行以下操作。

<FlatList
 ...
 keyExtractor={({matchId}, index) => matchId.toString()}
/> 

Alternatively you could use the index as the key and do this 或者,您可以使用索引作为关键字并执行此操作

<FlatList
 ...
 keyExtractor={({matchId}, index) => index.toString()}
/> 

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

相关问题 我该如何解决这个数据库问题?[json] - How can I solve this database problem?[json] 我该如何解决这个空的 dataframe 问题? - How can i solve this empty dataframe problem? 如何在本机反应中呈现 FlatList 中的数据,我可以控制台记录它但在屏幕上显示它时出现问题 - How to render data in FlatList in react native, I can Console log it but have a problem to display it on Screen 我该如何解决这个问题——“CategoryModel”的实例? Flutter - How can I solve this problem - Instance of 'CategoryModel'? Flutter 如何解决 Ansible 的 url 分页循环问题? - How can I solve the url pagination loop problem with Ansible? 将 json 发布到我的 web-api 的问题。 我该如何解决 - Problem with posting json to my web-api. How can i solve it 如何在渲染中检查元素为空(React Native) - How I can to check in the render that an element is empty (React Native) 我如何从获取API React Native获取响应 - How can i get response from fetch API React Native 如何将嵌套的JSON传递到我的DataSource…React Native - How Can I get Nested JSON to my DataSource … React Native react useEffect async triggering eachother 问题如何解决你们? - react useEffect async triggering eachother problem how solve you guys?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM