You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import { ImmutableListView } from 'react-native-immutable-list-view';
85
93
```
86
94
87
-
## Example Usage
95
+
## Example usage -- replacing FlatList
96
+
97
+
Goodbye, `keyExtractor` boilerplate!
98
+
99
+
> Note: This example diff looks much better on [GitHub](https://github.com/cooperka/react-native-immutable-list-view#example-usage----replacing-flatlist) than on npm's site.
100
+
> Red means delete, green means add.
101
+
102
+
```diff
103
+
-import { Text, View, FlatList } from 'react-native';
104
+
+import { Text, View } from 'react-native';
105
+
+import { ImmutableVirtualizedList } from 'react-native-immutable-list-view';
106
+
107
+
import style from './styles';
108
+
import listData from './listData';
109
+
110
+
class App extends Component {
111
+
112
+
renderItem({ item, index }) {
113
+
return <Text style={style.row}>{item}</Text>;
114
+
}
115
+
116
+
render() {
117
+
return (
118
+
<View style={style.container}>
119
+
<Text style={style.welcome}>
120
+
Welcome to React Native!
121
+
</Text>
122
+
- <FlatList
123
+
- data={listData}
124
+
- getItem={(items, index) => items.get(index)}
125
+
- getItemCount={(items) => items.size}
126
+
- keyExtractor={(item, index) => String(index)}
127
+
+ <ImmutableVirtualizedList
128
+
+ immutableData={listData}
129
+
renderItem={this.renderItem}
130
+
/>
131
+
</View>
132
+
);
133
+
}
134
+
135
+
}
136
+
```
137
+
138
+
## Example usage -- replacing ListView
88
139
89
140
You can remove all that boilerplate in your constructor, as well as lifecycle methods like
90
141
`componentWillReceiveProps` if all they're doing is updating your `dataSource`.
91
142
`ImmutableListView` will handle all of this for you.
92
143
93
-
Check out this example diff:
94
-
95
-
> Note: This looks much better on [GitHub](https://github.com/cooperka/react-native-immutable-list-view#example-usage) than on npm's site.
144
+
> Note: This example diff looks much better on [GitHub](https://github.com/cooperka/react-native-immutable-list-view#example-usage----replacing-listview) than on npm's site.
96
145
>Redmeansdelete, greenmeansadd.
97
146
98
147
```diff
@@ -156,29 +205,34 @@ Check out this example diff:
156
205
157
206
## Customization
158
207
159
-
AllthepropssupportedbyReactNative's `ListView` are simply passed through, and should work exactly the same.
160
-
You can read about them [here](https://facebook.github.io/react-native/docs/listview.html#props).
208
+
AllthepropssupportedbyReactNative's underlying List are simply passed through, and should work exactly the same.
209
+
You can see all the [VirtualizedList props](https://facebook.github.io/react-native/docs/virtualizedlist.html#props)
210
+
or [ListView props](https://facebook.github.io/react-native/docs/listview.html#props) on React Native'swebsite.
161
211
162
-
You can fully customize the look of your list by implementing [`renderRow`](https://facebook.github.io/react-native/docs/listview.html#renderrow)
Youcancustomizethelookofyourlistbyimplementing [`renderItem`](https://facebook.github.io/react-native/docs/flatlist.html#renderitem) for FlatList and VirtualizedList
213
+
or [`renderRow`](https://facebook.github.io/react-native/docs/listview.html#renderrow) for ListView.
164
214
165
-
Here are the additional props that `ImmutableListView` accepts:
215
+
Here are the additional props that `ImmutableVirtualizedList` and `ImmutableListView`accept:
166
216
167
217
| Prop name | Data type | Default value?| Description |
|`immutableData`| Any [`Immutable.Iterable`](https://facebook.github.io/immutable-js/docs/#/Iterable/isIterable) | Required. | The data to render. See below for some examples. |
170
220
|`rowsDuringInteraction`|`number`|`undefined`| How many rows of data to initially display while waiting for interactions to finish (e.g. Navigation animations). |
171
221
|`sectionHeaderHasChanged`|`func`|`(prevSectionData, nextSectionData) => false`| Only needed if your section header is dependent on your row data (uncommon; see [`ListViewDataSource`'s constructor](https://facebook.github.io/react-native/docs/listviewdatasource.html#constructor) for details). |
|`renderEmptyInList`|`string`or`func`|`'No data.'`|Ifyourdataisempty (e.g. `null`, `[]`, `{}`) andthispropisdefined, thenthiswillberenderedinstead (inside of an [`EmptyListView`](#emptylistview)). Pull-refreshandscrollingfunctionalitywillbe**kept**!|
222
+
| `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. |
223
+
| `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](#loading--empty--error-states) for more details. |
224
+
225
+
Also see [React Native's `FlatListExample`](https://github.com/facebook/react-native/blob/master/RNTester/js/FlatListExample.js)
This component takes an optional `emptyText` prop and renders an `ImmutableListView` with only a single list item with the text you specified.
239
-
By default, this string is simply `'No data.'`.
240
-
241
-
Example:
242
-
243
-
```jsx
244
-
import { ImmutableListView, EmptyListView } from 'react-native-immutable-list-view';
245
-
246
-
<ImmutableListView
247
-
immutableData={this.state.listData}
248
-
renderRow={this.renderRow}
249
-
renderEmpty={() => <EmptyListView emptyText="Nothing to see here!" />}
250
-
/>
251
-
```
276
+
Support is coming soon for section headers with`ImmutableVirtualizedList` too, similar to [`SectionList`](https://facebook.github.io/react-native/docs/sectionlist.html).
277
+
See [PR #34](https://github.com/cooperka/react-native-immutable-list-view/pull/34).
252
278
253
-
If you need more flexibility, instead of passing `emptyText` to `EmptyListView`, you can pass `renderRow` and display anything you want.
254
-
`EmptyListView` will pass all your props through to `ImmutableListView` (and then through to `ListView`).
279
+
## Loading / Empty /Error states
255
280
256
-
If you want to handle something like pull-refresh while empty, you can use the `originalProps`, e.g.:
281
+
The optional `renderEmptyInList` prop takes a string and renders an Immutable List displaying the text you specified.
282
+
By default, this text is simply `No data.`, but you can customize this based on your state. For example:
257
283
258
284
```jsx
259
-
<ImmutableListView
260
-
...
261
-
refreshControl={<RefreshControl ... />}
262
-
renderEmpty={(originalProps) =>
263
-
<EmptyListView
264
-
refreshControl={originalProps.refreshControl}
265
-
emptyText="Nothing to see here!"
285
+
render() {
286
+
const emptyText = this.state.isLoading
287
+
? "Loading..."
288
+
: this.state.errorMsg
289
+
? "Error!"
290
+
: "No data.";
291
+
292
+
return (
293
+
<ImmutableVirtualizedList
294
+
immutableData={this.state.listData}
295
+
renderItem={this.renderItem}
296
+
renderEmptyInList={emptyText}
266
297
/>
267
-
}
268
-
/>
269
-
```
270
-
271
-
## ImmutableVirtualizedList
272
-
273
-
Just as the `ImmutableListView` component helps render a `ListView` using Immutable data,
274
-
`ImmutableVirtualizedList` helps render the new and improved `VirtualizedList` component.
275
-
This is the underlying component that `FlatList` uses.
276
-
277
-
There's a [Medium article about it](https://medium.com/@cooperka/react-native-new-flatlist-component-30db558c7a5b) if you'd like more context.
278
-
The short version of the setup instructions is below:
279
-
280
-
#### With React Native v0.43 or higher
281
-
282
-
1. Make sure you're using at least `v0.5` of this library
283
-
2. Import the component:
284
-
285
-
```js
286
-
import { ImmutableVirtualizedList } from 'react-native-immutable-list-view';
287
-
```
288
-
289
-
#### With React Native v0.42 or lower
290
-
291
-
1. Make sure you're using `v0.4.x`ofthis library
292
-
2. Download the required files into your app's `node_modules` (since these components aren't published in a release quite yet):
or, using the [shell script](https://github.com/cooperka/react-native-immutable-list-view/blob/v0.4.5/bin/download-flatlist.sh) from this library:
297
-
298
-
```bash
299
-
npm run download-flatlist
300
-
```
301
-
302
-
3. Import the component directly:
303
-
304
-
```js
305
-
import ImmutableVirtualizedList from 'react-native-immutable-list-view/lib/ImmutableVirtualizedList';
306
-
```
307
-
308
-
#### All React Native versions
309
-
310
-
After following the above steps, simply render it:
311
-
312
-
```jsx
313
-
<ImmutableVirtualizedList
314
-
immutableData={this.state.listData}
315
-
renderItem={this.renderItem}
316
-
/>
298
+
);
299
+
}
317
300
```
318
301
319
-
See the [example app](https://github.com/cooperka/react-native-immutable-list-view/tree/master/example) for a working demo,
320
-
or [React Native's `FlatListExample`](https://github.com/facebook/react-native/blob/master/RNTester/js/FlatListExample.js) for an idea of what features are possible.
302
+
The empty list will receive all the same props as your normal list, so things like pull-to-refresh will still work.
0 commit comments