2017-10-06 45 views
1

我需要訪問有關react-native SectionList中renderItem節內部分(索引,值)的信息。根據http://docs.w3cub.com/react_native/sectionlist/#renderitem節的細節可以通過renderItem函數傳遞。但是在下面的代碼中,除了項目以外,所有其他值都將設置爲undefined。有沒有其他可能的方式來做到這一點?訪問來自反應本地SectionList中的節項的節數據

render(){ 
      return(
       <SectionList 
        sections={this.props.itemList} 
        renderItem={(item,section) => this._renderNewItem(item,section)} 
        renderSectionHeader={this._renderSectionHeader} 
        keyExtractor={(item) => item.id} 
       /> 
      ) 
     } 

_renderNewItem(item,section){ 
     console.log(item, " ", section) 
    } 

樣本數據結構

enter image description here

回答

1

renderItem丙傳遞給功能的參數。該參數是一個包含項目和部分數據的對象。

renderItem:(信息:{項目:項目,索引:數,節:SectionT, 隔板:{亮點:()=>空隙,unhighlight:()=>空隙, updateProps:(選擇: '領導' | '尾隨',newProps:對象)=> 無效,}})=> React.Element

爲了讓你可以使用它像部分數據低於

renderItem={({ item, section }) => this._renderNewItem(item,section)} 

更新

添加示例示例以演示其工作原理。 See it on snack.expo.io

import React, { Component } from 'react'; 
import { Text, View, StyleSheet, SectionList } from 'react-native'; 
import { Constants } from 'expo'; 
const data = [{key: 'New', data: [{name: 'Foo1'}, {name: 'Foo2'}]}, {key: 'Old', data: [{name:'Foo3'}, {name: 'Foo4'}]}]; 
export default class App extends Component { 

    _renderItem = ({item, section}) => (<Text>{`${item.name}(${section.key})`}</Text>) 

    _renderSectionHeader = ({section}) => { 
     return (
     <View style={styles.sectionHeader}> 
      <Text style={styles.header}>{section.key}</Text> 
     </View> 
    ) 
    } 

    render() { 
    return (
     <View style={styles.container}> 
     <SectionList 
      sections={data} 
      renderItem={this._renderItem} 
      renderSectionHeader={this._renderSectionHeader} 
     /> 
     </View> 
    ); 
    } 
} 

const styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    paddingTop: Constants.statusBarHeight, 
    backgroundColor: '#ecf0f1', 
    }, 
    sectionHeader: { 
     height: 50, 
     flex: 1, 
     backgroundColor: '#fff', 
     justifyContent: 'center', 
     paddingLeft: 10 
    }, 
    header: { 
     fontSize: 20, 
    } 
}); 
+0

我結果爲未定義 – Shanaka

+0

你可以添加樣本數據結構請 – bennygenel

+0

我已經加入上面的示例數據結構。鍵用於顯示部分標題和數據用於派生特定部分的行 – Shanaka