React Native:在一个SwipeListView中渲染不同类型的行

时间:2016-07-19 13:55:11

标签: listview reactjs react-native swipe react-native-listview

SwipeListView应该能够根据notification属性呈现2 +种不同的行:

  • true用于通知
  • false适用于常规商品

假设两种类型的行完全不同。我要解决的问题是如何使用允许向右和向左滑动行的SwipeListViewSwipeRow元素来渲染此类列表,而不是标准ListView

我总是遇到renderRow()renderHiddenRow()方法的问题,这些方法拒绝接受renderRow(data, secId, rowId, rowMap)返回的内容。

SwipeListView documentation

示例app:

import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View, ListView } from 'react-native';
import { SwipeListView, SwipeRow } from 'react-native-swipe-list-view'
var data = [ { id:0, notification: true, },{ id:1, notification: false, },{ id:2, notification: false, } ];

class SampleApp extends Component {

  renderRow(data, secId, rowId, rowMap) {

    var notificationRow = <SwipeRow disableRightSwipe={false} disableLeftSwipe={false} leftOpenValue={100} rightOpenValue={-100}>
      <View style={{flexDirection:'row',justifyContent:'space-between',alignItems:'center', left:0, right:0, paddingVertical:50,borderWidth:1, backgroundColor:'red'}}>
          <Text>Accept</Text><Text>Reject</Text>
      </View>
      <View>
          <Text style={{left:0, right:0, paddingVertical:50,borderWidth:1, backgroundColor:'green'}}>Notification</Text>
      </View>
    </SwipeRow>;

    var contentRow = <SwipeRow disableRightSwipe={true} disableLeftSwipe={false} leftOpenValue={100} rightOpenValue={-100}>
        <View style={{flexDirection:'row',justifyContent:'space-between',alignItems:'center', left:0, right:0, paddingVertical:50,borderWidth:1, backgroundColor:'red'}}>
            <Text>Edit</Text><Text>Delete</Text>
        </View>
        <View>
            <Text style={{left:0, right:0, paddingVertical:50,borderWidth:1, backgroundColor:'blue'}}>Row item</Text>
        </View>
      </SwipeRow>;

    if (data.notification) {
      return ({notificationRow});
    } else {
      return ({contentRow});
    }
  }

  render() {
    var ds = new ListView.DataSource({ rowHasChanged: (row1, row2) => row1 !== row2 });   
    return (
      <SwipeListView
        dataSource={ds.cloneWithRows(data)}
        renderRow={ (data, secId, rowId, rowMap) => {this.renderRow(data, secId, rowId, rowMap);}}
      />
    );
  }
}
AppRegistry.registerComponent('SampleApp', () => SampleApp);

最常见的错误:

  

SwipeListView.js:renderRow:67:undefined不是对象(评估   'Component.type')

Most common error

1 个答案:

答案 0 :(得分:1)

看起来return方法中缺少的部分是renderRow()renderRow={ (data, secId, rowId, rowMap) => {return this.renderRow(data, secId, rowId, rowMap);}}