Javascript 本机基如何获取List dataArray的ListItem索引?

Javascript 本机基如何获取List dataArray的ListItem索引?,javascript,reactjs,react-native,native-base,Javascript,Reactjs,React Native,Native Base,我想用dataArray访问从中生成的索引。 例如: <List dataArray={this.props.groceries} renderRow={(groceryListItem, index) => <ListItem> <Text>{index}</Text> </ListItem> } > </List> {index} } > 然而,这是行不通的。如何访

我想用dataArray访问从中生成的索引。 例如:

<List dataArray={this.props.groceries} renderRow={(groceryListItem, index) =>
    <ListItem>  
        <Text>{index}</Text>
    </ListItem>
    }
>
</List>

{index}
}
>
然而,这是行不通的。如何访问此动态索引? 谢谢

React-Native与您的
List
组件具有类似的结构,React-Native社区强烈建议使用
FlatList
,因为效率高,渲染项的
索引可以轻松访问。以下是一个简单的用法:

render() {
  return (
    <FlatList
      data={[
        {title: "title1"},
        {title: "title2"},
        {title: "title3"},
        {title: "title4"},
        {title: "title5"},
        {title: "title6"},
        {title: "title7"},
        {title: "title8"},
      ]}
      renderItem={(item) => <Text>{item.index}</Text>}
      keyExtractor={(item, index) => index.toString()}/>
  );
}
render(){
返回(
{item.index}
keyExtractor={(项,索引)=>index.toString()}/>
);
}
renderItem
中的
item
参数有两个重要属性

  • 索引:正在呈现的项的索引
  • 数据:在此示例中,您提供给平面列表的数据是传递给平面列表的
    data
    props的数组对象之一,例如
    {title:“title1”}
    ,我在
    renderItem
    方法中不使用此数据。您可以按如下方式使用它:
    data={this.props.groceries}
  • React-Native与您的
    列表
    组件具有类似的结构,React-Native社区强烈建议使用
    FlatList
    ,因为效率高,渲染项的
    索引
    很容易访问。以下是一个简单的用法:

    render() {
      return (
        <FlatList
          data={[
            {title: "title1"},
            {title: "title2"},
            {title: "title3"},
            {title: "title4"},
            {title: "title5"},
            {title: "title6"},
            {title: "title7"},
            {title: "title8"},
          ]}
          renderItem={(item) => <Text>{item.index}</Text>}
          keyExtractor={(item, index) => index.toString()}/>
      );
    }
    
    render(){
    返回(
    {item.index}
    keyExtractor={(项,索引)=>index.toString()}/>
    );
    }
    
    renderItem
    中的
    item
    参数有两个重要属性

  • 索引:正在呈现的项的索引
  • 数据:在此示例中,您提供给平面列表的数据是传递给平面列表的
    data
    props的数组对象之一,例如
    {title:“title1”}
    ,我在
    renderItem
    方法中不使用此数据。您可以按如下方式使用它:
    data={this.props.groceries}

  • 什么是列表?它是列表视图吗?如果是,你可以得到索引3。支柱

    <ListView 
    renderRow={(rowData, sectionID, index) => // you can get the index from 3.
    ...
    
    //您可以从3获取索引。
    ...
    
    什么是列表它是列表视图?如果是,你可以得到索引3。支柱

    <ListView 
    renderRow={(rowData, sectionID, index) => // you can get the index from 3.
    ...
    
    //您可以从3获取索引。
    ...
    
    我正在使用以下命令:

    <List dataArray={this.props.groceries} renderItem={({item, index}) =>
        <ListItem key={index}>  
            <Text>{item}</Text>
        </ListItem>
        }
    >
    </List>
    
    
    {item}
    }
    >
    
    我正在使用以下命令:

    <List dataArray={this.props.groceries} renderItem={({item, index}) =>
        <ListItem key={index}>  
            <Text>{item}</Text>
        </ListItem>
        }
    >
    </List>
    
    
    {item}
    }
    >
    
    您可以使用react native
    平面列表吗?索引可以在
    FlatList
    的renderItem方法中访问,您是否可以使用react native
    FlatList
    ?索引可以通过
    FlatList的renderItem方法访问,谢谢!这起作用了。列表是一个本机基本组件。好的,我知道了。我很高兴为你们解答我的问题谢谢!这起作用了。列表是一个本机基本组件。好的,我知道了。我很高兴为您解答我的问题