Giter Site home page Giter Site logo

cooperka / react-native-immutable-list-view Goto Github PK

View Code? Open in Web Editor NEW
205.0 6.0 34.0 1.48 MB

:scroll: Drop-in replacement for ListView, FlatList, and VirtualizedList.

License: MIT License

JavaScript 100.00%
react-native listview immutable immutablejs component flatlist virtualizedlist

react-native-immutable-list-view's Introduction

React Native Immutable ListView

Build status npm downloads npm version Latest GitHub tag


Logo


Drop-in replacement for React Native's ListView, FlatList, and VirtualizedList.

ImmutableListView screenshot

It supports Immutable.js to give you faster performance and less headaches.

Motivation

  • Do you use Immutable data, only to write the same boilerplate over and over in order to display it?
  • Do you want to show 'Loading...', 'No results', and 'Error!' states in your lists?
  • Do you have nested objects in your state so a shallow diff won't cut it for pure rendering?
  • Do you want better performance while animating screen transitions?

If you answered yes to ANY of these questions, this project can help. Check out the examples below.

How it works

For FlatList and VirtualizedList:

<ImmutableVirtualizedList
  immutableData={this.state.listData}
  renderItem={this.renderItem}
/>

For ListView (deprecated as of React Native v0.59):

<ImmutableListView
  immutableData={this.state.listData}
  renderRow={this.renderRow}
/>

The screenshot above shows two different lists. The first uses this data:

Immutable.fromJS({
  'Section A': [
    'foo',
    'bar',
  ],
  'Section B': [
    'fizz',
    'buzz',
  ],
})

The second list is even simpler:

Immutable.Range(1, 100)

There's an example app here if you'd like to see it in action.

Installation

  1. Install:

    • Using npm: npm install react-native-immutable-list-view --save
    • Using Yarn: yarn add react-native-immutable-list-view
  2. Import it in your JS:

    For FlatList and VirtualizedList:

    import { ImmutableVirtualizedList } from 'react-native-immutable-list-view';

    For ListView:

    import { ImmutableListView } from 'react-native-immutable-list-view/lib/ImmutableListView';

Example usage -- replacing FlatList

Goodbye, keyExtractor boilerplate!

Note: This example diff looks much better on GitHub than on npm's site. Red means delete, green means add.

-import { Text, View, FlatList } from 'react-native';
+import { Text, View } from 'react-native';
+import { ImmutableVirtualizedList } from 'react-native-immutable-list-view';

 import style from './styles';
 import listData from './listData';

 class App extends Component {

   renderItem({ item, index }) {
     return <Text style={style.row}>{item}</Text>;
   }

  render() {
    return (
      <View style={style.container}>
         <Text style={style.welcome}>
           Welcome to React Native!
         </Text>
-        <FlatList
-          data={listData}
-          getItem={(items, index) => items.get(index)}
-          getItemCount={(items) => items.size}
-          keyExtractor={(item, index) => String(index)}
+        <ImmutableVirtualizedList
+          immutableData={listData}
           renderItem={this.renderItem}
         />
      </View>
    );
  }

}

Example usage -- replacing ListView

You can remove all that boilerplate in your constructor, as well as lifecycle methods like componentWillReceiveProps if all they're doing is updating your dataSource. ImmutableListView will handle all of this for you.

Note: This example diff looks much better on GitHub than on npm's site. Red means delete, green means add.

-import { Text, View, ListView } from 'react-native';
+import { Text, View } from 'react-native';
+import { ImmutableListView } from 'react-native-immutable-list-view/lib/ImmutableListView';

 import style from './styles';
 import listData from './listData';

 class App extends Component {

-  constructor(props) {
-    super(props);
-
-    const dataSource = new ListView.DataSource({
-      rowHasChanged: (r1, r2) => r1 !== r2,
-      sectionHeaderHasChanged: (s1, s2) => s1 !== s2,
-    });
-
-    const mutableData = listData.toJS();
-
-    this.state = {
-      dataSource: dataSource.cloneWithRowsAndSections(mutableData),
-    };
-  }
-
-  componentWillReceiveProps(newProps) {
-    this.setState({
-      dataSource: this.state.dataSource.cloneWithRows(newProps.listData),
-    });
-  }
-
   renderRow(rowData) {
     return <Text style={style.row}>{rowData}</Text>;
   }

  renderSectionHeader(sectionData, category) {
    return <Text style={style.header}>{category}</Text>;
  }

  render() {
    return (
      <View style={style.container}>
         <Text style={style.welcome}>
           Welcome to React Native!
         </Text>
-        <ListView
-          dataSource={this.state.dataSource}
+        <ImmutableListView
+          immutableData={listData}
           renderRow={this.renderRow}
           renderSectionHeader={this.renderSectionHeader}
         />
      </View>
    );
  }

}

Customization

All the props supported by React Native's underlying List are simply passed through, and should work exactly the same. You can see all the VirtualizedList props or ListView props on React Native's website.

You can customize the look of your list by implementing renderItem for FlatList and VirtualizedList or renderRow for ListView.

Here are the additional props that ImmutableVirtualizedList and ImmutableListView accept:

Prop name Data type Default value? Description
immutableData Any Immutable.Iterable Required. The data to render. See below for some examples.
rowsDuringInteraction number undefined How many rows of data to initially display while waiting for interactions to finish (e.g. Navigation animations).
sectionHeaderHasChanged func (prevSectionData, nextSectionData) => false Only needed if your section header is dependent on your row data (uncommon; see ListViewDataSource's constructor for details).
renderEmpty string or func undefined If your data is empty (e.g. null, [], {}) and this prop is defined, then this will be rendered instead. Pull-refresh and scrolling functionality will be lost. Most of the time you should use renderEmptyInList instead.
renderEmptyInList string or func 'No data.' If your data is empty (e.g. null, [], {}) and this prop is defined, then this will be rendered instead. Pull-refresh and scrolling functionality will be kept! See below for more details.

Also see React Native's FlatListExample for more inspiration.

Methods

Methods such as scrollToEnd are passed through just like the props described above. You can read about them here for ListView or here for FlatList and VirtualizedList.

The references to the raw VirtualizedList or ListView component are available via getVirtualizedList() or getListView(). These references allow you to access any other methods on the underlying List that you might need.

How to format your data

ImmutableListView accepts several standard formats for list data. Here are some examples:

List

[rowData1, rowData2, ...]

Map of Lists

{
    section1: [
        rowData1,
        rowData2,
        ...
    ],
    ...
}

Map of Maps

{
    section1: {
        rowId1: rowData1,
        rowId2: rowData2,
        ...
    },
    ...
}

To try it out yourself, you can use the example app!

Support is coming soon for section headers with ImmutableVirtualizedList too, similar to SectionList. See PR #34.

Loading / Empty / Error states

The optional renderEmptyInList prop takes a string and renders an Immutable List displaying the text you specified. By default, this text is simply No data., but you can customize this based on your state. For example:

render() {
  const emptyText = this.state.isLoading
    ? "Loading..."
    : this.state.errorMsg
      ? "Error!"
      : "No data.";

  return (
    <ImmutableVirtualizedList
      immutableData={this.state.listData}
      renderItem={this.renderItem}
      renderEmptyInList={emptyText}
    />
  );
}

The empty list will receive all the same props as your normal list, so things like pull-to-refresh will still work.

react-native-immutable-list-view's People

Contributors

cooperka avatar jaulz avatar jephuff avatar magrinj avatar mammad2c avatar zzq889 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

react-native-immutable-list-view's Issues

React Native 0.60 support

ListView has been removed from React Native.
Now that ListView has been deprecated, are there plans to remove it from this library so that it can be used in 0.60 builds?

Example of scrollTo usage

I'm trying to use scrollTo with the component and its saying that the method is not implemented. Could you provide an example?

--NVM just needed to update my package.json thanks!

Immutable.List of length 0 causes UI to freeze

Hello, encountered an interesting bug while using ImmutableVirtualizedList. Add the following stateless, functional component to your render tree, and will bring your react-native app's UI thread to its knees. With no console error, no nothing.

import Immutable from 'immutable';
import ImmutableVirtualizedList from 'react-native-immutable-list-view/lib/ImmutableVirtualizedList';
import React from 'react';

function HomeHelper() {
  const list = new Immutable.List();
  return (
    <ImmutableVirtualizedList
      immutableData={list}
      renderItem={() => (
        <View/>
      )}
    />
  );
}

Currently using these dependency versions:

[email protected]
[email protected]
[email protected]

ui_broken

Using the accessibility inspector to better illustrate the broken UI. Normally the clicks would switch between tabs.

Publish new version that uses stable VirtualizedList

The stable version of VirtualizedList is available in the published version of React Native as of v0.43 (under Libraries/CustomComponents/Lists). I plan to make a major version update to support this and then ANOTHER major version update to support v0.44 after the component moves directories again (under Libraries/Lists).

The final update to use Libraries/Lists is planned to be version 1.0.0 of this module :)

Iterable.length warnings with ImmutableVirtualizedList

Hello there !

I'm getting following warnings when using ImmutableVirtualizedList

YellowBox.js:71 iterable.length has been deprecated, use iterable.size or iterable.count(). This warning will become a silent error in a future version. Error
    at Object.getItemCount (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:96434:37)
    at VirtualizedList.render (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false&minify=false:40071:34)

It seems to be linked to this line:
getItemCount={(items) => (items.size || items.length || 0)}

Should we remove items.length removed as warning says ?

Stack:
immutable: ^3.8.1
react-native-immutable-list-view: ^0.5.0

How to update data?

I am new with mutable data,

How to update row in this listView?

I tried to use map and than reset state.. it`s not working

select(filter) {
        const dataSet = this.state.dataSet.map((obj) => {
            obj.selected = isEqual(obj, filter);
            return obj;
        });

        console.log(dataSet.toJS());
        this.setState({
            dataSet
        })
    }

dataSet updated.. but listVIew.. not rerender new row... how to deal with this?

rendering new rows

What is the best approach to avoid re-rendering all rows when new elements get added to the data source?
I tried using List, but renderRow gets called for all existing rows when new item is pushed.
Using Map solves this issue but I don't know how to order it. In original ListView I was using
identities to sort the data set. Like this:
cloneWithRows(data, identities)

Thank you.

Make consistent FlatList require

First of all, thanks for such a great component!

I found your lib through your medium blog post, and installed FlatList using provided bash which installs to the .../CustomComponents/Lists, but this component requires it from node_modules/react-native/Libraries/Experimental/, maybe we should change the FlatList import path to the CustomComponents/Lists as the install path from blog?

More social media activity

This repo seems to have only moderate usage, but also a higher stars/user ratio than average, which tells me that the people using it find it helpful, but it's only being discovered by people who already know exactly what they need.

I wrote an article about it on Medium, which also has high engagement but a low number of total views. SEO is pretty low when searching for either one; the article doesn't even show up in Google results when searching for the exact title.

If anyone has ideas for how to improve visibility, I'm all ears! 🐱

immutable as dev dependency

Hi, thanks for the work on this package.

I was curious if it were possible to make immutable a devDependency in your package.json. It seems react-native already specifies it, albeit an older version.

Sorted Immutable Map Not Rendering Correctly

I have an immutable map that looks like:

data = {
---header1: {
------prop1:
------prop2:
------etc...
---}
---header2: {
------etc...
---}
}

I can't figure out how to get the headers to show up in the correct order with this library. i.e.

header1
header1 data
header2
header2 data

They are currently displaying

header2
data
header1
data

Do I need to set a section ID somewhere?

Row will not be refreshed

RowHasChanged whether this function can be made public。
becase when I change object , but row will not be refreshed.
I think Immutable.is can not meet my requirements

Available to accept `seamless-immutable`

For now, i can't use this component with seamless-immutable.
I'm getting immutableData.isEmpty is not defined error.
Since seemless-immutable is also widely used immutable library, can we also support this?

empty ImmutableVirtualizedList always generates a missing key prop warning

version: 0.6.2

If my ImmutableVirtualizedList is empty, I will see this warning:

ExceptionsManager.js:73 Warning: Each child in an array or iterator should have a unique "key" prop.

Check the render method of `VirtualizedList`. See https://fb.me/react-warning-keys for more information.
    in CellRenderer (at VirtualizedList.js:499)
    in VirtualizedList (at ImmutableVirtualizedList.js:109)
    in ImmutableVirtualizedList (at EmptyVirtualizedList.js:76)
    in EmptyVirtualizedList (at ImmutableVirtualizedList.js:102)
    in ImmutableVirtualizedList (at CpTeamList.js:19)
    in RCTView (at View.js:113)
    in View (at CpTeamList.js:18)
    in CpTeamList (at CtTeamList.js:58)

I've tried implementing my own renderEmptyInList that provides a key on the rendered component, but the warning remains.

Code:

 render() {
    return (
      <View style={styles.container}>
        <ImmutableVirtualizedList
          immutableData={List()}
          ItemSeparatorComponent={this.renderSeparator}
          keyExtractor={(item) => item.get("id")}
          /*renderEmptyInList={this.renderEmptyInList}*/
          renderItem={this.renderItem}
        />
      </View>
    )
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.