Skip to content

Commit a6da9f3

Browse files
committed
feat(Init): First release
1 parent e034fa3 commit a6da9f3

File tree

6 files changed

+207
-1
lines changed

6 files changed

+207
-1
lines changed

.travis.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
language: node_js
2+
cache:
3+
directories:
4+
- node_modules
5+
notifications:
6+
email: false
7+
node_js:
8+
- '7'
9+
- '6'
10+
- '4'
11+
before_script:
12+
- npm prune
13+
after_success:
14+
- npm run semantic-release
15+
branches:
16+
except:
17+
- /^v\d+\.\d+\.\d+$/

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
11
# react-native-optimized-flatlist
2-
Optimization for complex and slow React Native FlatLists
2+
![](https://img.shields.io/npm/v/react-native-optimized-flatlist.svg)
3+
4+
__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! :)__
5+
6+
Please also read more about the issue here:
7+
https://github.com/facebook/react-native/issues/13413
8+
9+
## Installation
10+
```
11+
npm i -S react-native-optimized-flatlist
12+
```
13+
or
14+
```
15+
yarn add react-native-optimized-flatlist
16+
```
17+
18+
19+
## Usage
20+
Just replace `FlatList` with `OptimizedFlatList` .. thats all! :)
21+
22+
Replace this:
23+
```js
24+
render() {
25+
return (
26+
<FlatList
27+
data={[{name: 'fred', name: 'freddy'}]}
28+
renderItem={ ({item}) => <View>{item.name}</View>}
29+
/>
30+
...
31+
```
32+
With this:
33+
```js
34+
...
35+
import {OptimizedFlatList} from 'react-native-optimized-flatlist'
36+
37+
...
38+
render() {
39+
return (
40+
<OptimizedFlatList
41+
data={[{name: 'fred', name: 'freddy'}]}
42+
renderItem={ ({item}) => <View>{item.name}</View>}
43+
/>
44+
...
45+
46+
```

package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "react-native-optimized-flatlist",
3+
"version": "0.0.0-development",
4+
"description": "Optimization for complex and slow React Native FlatLists",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 0",
8+
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "https://github.com/stoffern/react-native-optimized-flatlist.git"
13+
},
14+
"keywords": [
15+
"react-native",
16+
"flatlist"
17+
],
18+
"author": "stoffern",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/stoffern/react-native-optimized-flatlist/issues"
22+
},
23+
"homepage": "https://github.com/stoffern/react-native-optimized-flatlist#readme",
24+
"devDependencies": {
25+
"cz-conventional-changelog": "^2.0.0",
26+
"semantic-release": "^6.3.2"
27+
},
28+
"config": {
29+
"commitizen": {
30+
"path": "./node_modules/cz-conventional-changelog"
31+
}
32+
}
33+
}

src/FlatListItem.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from 'react'
2+
import {
3+
View,
4+
} from 'react-native'
5+
6+
7+
export default class FlatListItem extends React.PureComponent {
8+
static propTypes = {
9+
viewComponent: React.PropTypes.element.isRequired
10+
};
11+
12+
constructor(props) {
13+
super(props);
14+
this.state = {
15+
visibility: true,
16+
}
17+
}
18+
19+
onLayout(evt) {
20+
this.viewProperties = {
21+
width: 0,
22+
height: 0,
23+
}
24+
this.viewProperties.width = evt.nativeEvent.layout.width;
25+
this.viewProperties.height = evt.nativeEvent.layout.height;
26+
}
27+
28+
setVisibility(visibility) {
29+
if (this.state.visibility != visibility) {
30+
if (visibility == true) this.setState({ visibility: true })
31+
else this.setState({ visibility: false })
32+
}
33+
}
34+
35+
render() {
36+
if (this.state.visibility === false) {
37+
return ( <View style={{ width: this.viewProperties.width, height: this.viewProperties.height }} /> )
38+
}
39+
40+
return (
41+
<View onLayout={this.onLayout.bind(this)}>
42+
{this.props.viewComponent}
43+
</View>
44+
)
45+
}
46+
}

src/OptimizedFlatList.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React from 'react'
2+
import {
3+
FlatList,
4+
} from 'react-native'
5+
6+
import ListItem from './ListItem'
7+
8+
export default class OptimizedFlatList extends React.PureComponent {
9+
10+
constructor(props) {
11+
super(props);
12+
this.state = {}
13+
this.rowRefs =[]
14+
}
15+
16+
_addRowRefs(ref, data){
17+
this.rowRefs[data.index] = {
18+
ref: ref,
19+
item: data.item,
20+
index: data.index,
21+
}
22+
}
23+
24+
_updateItem(index, visibility){
25+
this.rowRefs[index].ref.setVisibility(visibility)
26+
return visibility
27+
}
28+
29+
_renderItem(data){
30+
const view = this.props.renderItem(data)
31+
return (
32+
<ListItem
33+
ref={ myItem => this._addRowRefs(myItem, data)}
34+
viewComponent={view}
35+
data={data}
36+
/>
37+
)
38+
}
39+
40+
_onViewableItemsChanged (info: {
41+
changed: Array<{
42+
key: string,
43+
isViewable: boolean,
44+
item: any,
45+
index: ?number,
46+
section?: any,
47+
}>
48+
}
49+
) {
50+
info.changed.map(item =>
51+
this._updateItem(item.index, item.isViewable)
52+
)
53+
}
54+
55+
render() {
56+
return (
57+
<FlatList
58+
{...this.props}
59+
renderItem={ data => this._renderItem(data) }
60+
onViewableItemsChanged={this._onViewableItemsChanged.bind(this)}
61+
/>
62+
)
63+
}
64+
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export {default as OptimizedFlatList} from './OptimizedFlatList'
2+
export {default as FlatListItem} from './FlatListItem'

0 commit comments

Comments
 (0)