Giter Club home page Giter Club logo

react-native-controlled-listview's Introduction

react-native-controlled-listview

npm version

The standard React Native ListView you know and love, with a declarative Flux-friendly API


Why?

For performance reasons, React Native ListView needs a ListView.DataSource, so it can efficiently update itself. To benefit from these optimisations, any component wishing to render a ListView needs to be stateful to hold the DataSource, and faff about with lifecycle methods to update it.

This library hides that statefulness and provides a simple, props-based API to render ListViews.

How-to

Installation:

npm i --save react-native-controlled-listview

Instead of dataSource, controlled ListView expects an array prop items. Optionally, you can sort the list with sortBy or group it into sections with sectionBy:

- import { ListView } from 'react-native';
+ import ListView from 'react-native-controlled-listview';

// stateless function component
export default (props) => (
+ <ListView
-   dataSource=...
+   items={props.people}
+   sortBy={(person) => person.lastName}
+   sectionBy={(person) => person.lastName[0]}
    renderRow={(person) => (
      <Text style={styles.row}>{person.lastName}, {person.firstName}</Text>
    )}
    renderSectionHeader={(sectionData, initial) => (
      <Text style={styles.sectionHeader}>{initial}</Text>  
    )}
  />
);

Immutability

There is one gotcha. This component expects you to clone the items prop when you want to ListView to update. If you are using Redux, this should already be the case.

The items prop can be an instance of Immutable.List, or an array. If using plain arrays, never mutate it in-place, or the ListView won't update.

See dataSourceShouldUpdate on how to customise the update logic.

Props

items : any[] | Immutable.List (required)

List data source.

sortBy : (a, b) => number | boolean

Sorts the list based on a comparator. Value can be one of type:

  • (a, b) => number a standard Array#sort compareFunction.
  • (a, b) => boolean a shorthand comparator: if returns true, a comes first; if false, b comes first.
sectionBy : (a, b) => string

Groups the list based on returned value and renders section headers for each group.

If using sectionBy, you must also provide renderSectionHeader

rowHasChanged : (prevItem, nextItem) => boolean

Passed directly to ListView.DataSource. constructor. Defaults to !Immutable.is(prevItem, nextItem), which performs a === comparison for plain objects.

sectionHeaderHasChanged : (prevSectionData, nextSectionData) => boolean

Passed directly to ListView.DataSource. constructor. Defaults to prev !== next.

dataSourceShouldUpdate : (prevProps, nextProps) => boolean

Controls when the data source should be updated. The default implementation is !Immutable.is(prevProps.items, nextProps.items), which performs a === comparison for plain arrays.

All other properties, except dataSource are passed directly to the underlying ListView.

Please note

This project is in a pre-release state. The API may be considered relatively stable, but changes may still occur.

MIT licensed

react-native-controlled-listview's People

Contributors

jevakallio avatar

Watchers

 avatar

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.