简体   繁体   English

React Native:如何在没有 id 的情况下呈现 FlatList 中的获取数据

[英]React Native: How to render fetch data in FlatList without id

I'm trying to render data from an API but it does not have an id or any unique data to distinguish the item我正在尝试从 API 呈现数据,但它没有 id 或任何唯一数据来区分项目

This is how the data looks like when I do console.log这就是我执行 console.log 时数据的样子

Array [
  Object {
    "photo1": "www.picexample.com",
  },
  Object {
    "photo2": "www.picexample.com",
  },
]

This is my code:这是我的代码:

Home.js主页.js

const [portfolio, setPortfolio] = React.useState([]);
const renderItem= ({item}) => {
    return (
        <View>
            <Image source={{uri: ?}} resizeMode="cover" />
        </View>
    );
}
useEffect (() => {
        .then((result) => {
            setPortfolio(result.myImage); 
            console.log(portfolio);
        })
});
return (
    <ScrollView>
        <FlatList
            scrollEnabled={false}
            data={portfolio}
            renderItem={renderItem} 
            keyExtractor={?}
        />
    </ScrollView>
);

UPDATE (Based on Joel Hager)更新(基于 Joel Hager)

const keyExtractor = (portfolio, idx) => `${Object.keys(portfolio)}-${idx}`
const renderItem = ({item}) => {
    console.log(item);
    return (
        <View>
            <Image source={{uri: item}} resizeMode="cover" />
        </View>
    );
}
return (
    <FlatList
        scrollEnabled={false}
        data={portfolio}
        renderItem={renderItem} 
        keyExtractor={keyExtractor}
    />
);

Without being able to post working React Native code, this will explain what it's doing conceptually.由于无法发布工作的 React Native 代码,这将在概念上解释它在做什么。

It's taking the item instance in the array that's being passed (the 'item') It's grabbing all keys for it: Object.keys() It's displaying the first one它获取正在传递的数组中的项目实例(“项目”)它正在为它获取所有键: Object.keys()它正在显示第一个

There are some caveats: It expects a value.有一些警告:它需要一个值。 You could always use null coalescence to do something else, but I'd have to know more about the data to cover those cases.您总是可以使用空合并来做其他事情,但我必须更多地了解数据才能涵盖这些情况。 If you always get a key, you'll always get a value.如果你总是得到一个键,你总是会得到一个值。 You can also add the index value in the extractor to give it some more specificity.您还可以在提取器中添加索引值以使其更具特异性。 Your react code should look like this:你的反应代码应该是这样的:

keyExtractor={(item, idx) => `${Object.keys(item)}-${idx}`}

note: It's always good to extract the function outside of the flatlist so it isn't recreated every render.注意:在平面列表之外提取函数总是好的,这样就不会在每次渲染时都重新创建它。 So it would look something like this:所以它看起来像这样:

const keyExtractor = (item, idx) => `${Object.keys(item)}-${idx}`
...
<Flatlist
...
keyExtractor={keyExractor}
...

More info on keyExtractor: https://reactnative.dev/docs/flatlist#keyextractor有关 keyExtractor 的更多信息: https ://reactnative.dev/docs/flatlist#keyextractor

The template literal will put the value of the string of the key and the index with a '-' between them.模板文字会将键的字符串的值和索引的值放在它们之间。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

 const payload = [ { "photo1": "www.picexample.com", }, { "photo2": "www.picexample.com", }, ] payload.map((item, idx) => console.log(Object.keys(item)[0] + `-${idx}`));

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

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