Skip to content

Commit

Permalink
feat(Init): First release
Browse files Browse the repository at this point in the history
  • Loading branch information
st0ffern committed May 3, 2017
1 parent e034fa3 commit a6da9f3
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: node_js
cache:
directories:
- node_modules
notifications:
email: false
node_js:
- '7'
- '6'
- '4'
before_script:
- npm prune
after_success:
- npm run semantic-release
branches:
except:
- /^v\d+\.\d+\.\d+$/
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,46 @@
# react-native-optimized-flatlist
Optimization for complex and slow React Native FlatLists
![](https://img.shields.io/npm/v/react-native-optimized-flatlist.svg)

__Optimization for FlatLists. This is a fix over the FlatList that removed every item that is not inside the viewport. This will give a more stable and faster FlatList as performance dont drop! :)__

Please also read more about the issue here:
https://github.com/facebook/react-native/issues/13413

## Installation
```
npm i -S react-native-optimized-flatlist
```
or
```
yarn add react-native-optimized-flatlist
```


## Usage
Just replace `FlatList` with `OptimizedFlatList` .. thats all! :)

Replace this:
```js
render() {
return (
<FlatList
data={[{name: 'fred', name: 'freddy'}]}
renderItem={ ({item}) => <View>{item.name}</View>}
/>
...
```
With this:
```js
...
import {OptimizedFlatList} from 'react-native-optimized-flatlist'

...
render() {
return (
<OptimizedFlatList
data={[{name: 'fred', name: 'freddy'}]}
renderItem={ ({item}) => <View>{item.name}</View>}
/>
...

```
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "react-native-optimized-flatlist",
"version": "0.0.0-development",
"description": "Optimization for complex and slow React Native FlatLists",
"main": "src/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 0",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"repository": {
"type": "git",
"url": "https://github.com/stoffern/react-native-optimized-flatlist.git"
},
"keywords": [
"react-native",
"flatlist"
],
"author": "stoffern",
"license": "MIT",
"bugs": {
"url": "https://github.com/stoffern/react-native-optimized-flatlist/issues"
},
"homepage": "https://github.com/stoffern/react-native-optimized-flatlist#readme",
"devDependencies": {
"cz-conventional-changelog": "^2.0.0",
"semantic-release": "^6.3.2"
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
}
}
46 changes: 46 additions & 0 deletions src/FlatListItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react'
import {
View,
} from 'react-native'


export default class FlatListItem extends React.PureComponent {
static propTypes = {
viewComponent: React.PropTypes.element.isRequired
};

constructor(props) {
super(props);
this.state = {
visibility: true,
}
}

onLayout(evt) {
this.viewProperties = {
width: 0,
height: 0,
}
this.viewProperties.width = evt.nativeEvent.layout.width;
this.viewProperties.height = evt.nativeEvent.layout.height;
}

setVisibility(visibility) {
if (this.state.visibility != visibility) {
if (visibility == true) this.setState({ visibility: true })
else this.setState({ visibility: false })
}
}

render() {
if (this.state.visibility === false) {
return ( <View style={{ width: this.viewProperties.width, height: this.viewProperties.height }} /> )
}

return (
<View onLayout={this.onLayout.bind(this)}>
{this.props.viewComponent}
</View>
)
}
}
64 changes: 64 additions & 0 deletions src/OptimizedFlatList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React from 'react'
import {
FlatList,
} from 'react-native'

import ListItem from './ListItem'

export default class OptimizedFlatList extends React.PureComponent {

constructor(props) {
super(props);
this.state = {}
this.rowRefs =[]
}

_addRowRefs(ref, data){
this.rowRefs[data.index] = {
ref: ref,
item: data.item,
index: data.index,
}
}

_updateItem(index, visibility){
this.rowRefs[index].ref.setVisibility(visibility)
return visibility
}

_renderItem(data){
const view = this.props.renderItem(data)
return (
<ListItem
ref={ myItem => this._addRowRefs(myItem, data)}
viewComponent={view}
data={data}
/>
)
}

_onViewableItemsChanged (info: {
changed: Array<{
key: string,
isViewable: boolean,
item: any,
index: ?number,
section?: any,
}>
}
) {
info.changed.map(item =>
this._updateItem(item.index, item.isViewable)
)
}

render() {
return (
<FlatList
{...this.props}
renderItem={ data => this._renderItem(data) }
onViewableItemsChanged={this._onViewableItemsChanged.bind(this)}
/>
)
}
}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {default as OptimizedFlatList} from './OptimizedFlatList'
export {default as FlatListItem} from './FlatListItem'

0 comments on commit a6da9f3

Please sign in to comment.