Search code examples
listviewreactjsreact-nativeswipereact-native-listview

React Native: Render different types of rows in one SwipeListView


This SwipeListView should be able to render 2+ different kinds of rows based on the notification attribute:

  • true is for notifications
  • false is for regular items

Assume that the two types of rows are entirely different. The problem that I am trying to solve is how to render such a list using the SwipeListView and SwipeRow elements that allows swiping rows right and left, not the standard ListView.

I always get into problem with renderRow() and renderHiddenRow() methods that refuse to take what renderRow(data, secId, rowId, rowMap) returns.

SwipeListView documentation

Example 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);

Most common error:

SwipeListView.js:renderRow:67: undefined is not an object (evaluating 'Component.type')

Most common error


Solution

  • It looks like the missing part was return inside the renderRow() method. renderRow={ (data, secId, rowId, rowMap) => {return this.renderRow(data, secId, rowId, rowMap);}}