React Native VirtualizedList不绑定getItemCount和renderItem

时间:2019-09-22 20:53:04

标签: react-native

我试图使用绑定到该组件的_getItemCount_renderItem函数来定义我的组件,但是无论我以何种方式用托盘绑定它们,VirtualizedList都会出错。但是,我可以绑定_keyExtractor

class MyCrud extends Component {
    _renderItem = ({item, index}) => {
        console.log('CategoryCrud::renderItem(item=', item, ')');
        let id = item.id;
        return (
        <View key={id} style={styles.row}>
            <Text style={styles.boldLabel}>Código</Text>
            <Text>{item.attributes.code}</Text>
            <Text style={styles.label}>Nombre</Text>
            <Text>{item.attributes.name}</Text>
        </View>
        )
    }   

    _keyExtractor = (item, index) => item.id.toString();

    render() {
        console.log('CategoryCrud::render(props=', this.props,')');
        return (
            <View style={styles.container}>
                <VirtualizedList
                contentContainerStyle={styles.listContent}
                data={categories.data}
                getItem = { (data, index) => data[index] }
                getItemCount = {(data) => data.length }
                keyExtractor = { this._keyExtractor }
                renderItem = { this._renderItem }
                />
            </View>
        )
    }
} // component

这给了我以下错误:

[22:39:46] E | ReactNativeJS ▶ Invariant Violation: no renderItem!
                             │ 
                             │ This error is located at:
                             │ in CellRenderer (at VirtualizedList.js:688)
                             │ in RCTView (at View.js:35)
                             │ in View (at ScrollView.js:1007)
                             │ in RCTScrollView (at ScrollView.js:1147)
                             │ in ScrollView (at VirtualizedList.js:1081)
                             │ in VirtualizedList (at category_index.js:116)

但是,如果我将renderItem函数放在props的{​​{1}}中,它将起作用:

VirtualizedList

当我尝试绑定 return ( <View style={styles.container}> <VirtualizedList contentContainerStyle={styles.listContent} data={categories.data} getItem = { (data, index) => data[index] } getItemCount = { this._getItemCount} keyExtractor = {(item, index) => index.toString() } renderItem = {({item, index}) => { let id = item.id; return ( <View key={id} style={styles.row}> <Text style={styles.boldLabel}>Código</Text> <Text>{item.attributes.code}</Text> <Text style={styles.label}>Nombre</Text> <Text>{item.attributes.name}</Text> </View> ) }} 函数时,

getItemCount

然后

    _getItemCount(data) {
        console.log('CategoryCrud::getItem(categories=', this.props.categories, ')');
    }

错误不同:

    render() {
        return (
        <View style={styles.container}>
            <VirtualizedList
            contentContainerStyle={styles.listContent}
            data={categories.data}
            getItem = { (data, index) => data[index] }
            getItemCount = { this._getItemCount}

是否像上面那样定义[22:44:22] E | ReactNativeJS ▶ TypeError: _this.props.getItemCount is not a function. (In '_this.props.getItemCount(_this.props.data)', '_this.props.getItemCount' is undefined) 函数还是这样

getItemCount

如果我不在组件中使用函数并将其放在 constructor(props) { super(props); this._getItemCount = this._getItemCount.bind(this); } _getItemCount(data) { console.log('CategoryCrud::getItem(categories=', this.props.categories, ')'); } 属性中,则一切正常,但是我需要能够将这些函数用作组件的方法。

0 个答案:

没有答案