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
Copy file name to clipboardExpand all lines: docs/Tutorial.md
+201-8Lines changed: 201 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,7 @@ next: videos
11
11
12
12
This is a tutorial that aims to get you up to speed with writing iOS apps using React Native. If you want to learn what React Native is and why Facebook built it, check out this blog post: **[INSERT BLOG POST URL]**.
13
13
14
-
We assume you have experience writing websites with ReactJS. If not, you can learn about ReactJS[here](http://facebook.github.io/react/).
14
+
We assume you have experience writing websites with React. If not, you can learn about React[here](http://facebook.github.io/react/).
15
15
16
16
17
17
## Setup
@@ -109,6 +109,10 @@ And lastly we need to apply this still to the Image component:
Require the fetch module which is used to make an HTTP request to rotten tomatoes's API.
189
+
### Fetching real data
186
190
187
-
```javascript
188
-
var fetch =require('fetch');
189
-
```
191
+
Fetching data from Rotten Tomatoes's API isn't really relevant to learning React Native so feel free to breeze through this section.
190
192
191
193
Add the following constants to the top of the file (typically below the requires) to create the REQUEST_URL used to request data with.
192
194
@@ -203,7 +205,7 @@ Add some initial state to our application so that we can check this.state.movies
203
205
```javascript
204
206
getInitialState:function() {
205
207
return {
206
-
movies:null
208
+
movies:null,
207
209
};
208
210
},
209
211
```
@@ -272,6 +274,197 @@ Now modify the render function to render a loading view if we don't have any mov
272
274
273
275
Now press cmd+R and you should see "Loading movies..." until the response comes back, then it will render the first movie it fetched from Rotten Tomatoes.
Let’s now modify this application to render all of this data in a ListView, rather than just the first movie.
284
+
285
+
Why is a ListView better than just rendering all of these elements or putting them in a ScrollView? Despite React being fast, rendering a possibly infinite list of elements could be slow. ListView schedules rendering of views so that you only display the ones on screen and those already rendered but off screen are removed from the hierarchy.
286
+
287
+
First thing's first, add the ListView require to the top of the file.
288
+
289
+
```javascript
290
+
var {
291
+
AppRegistry,
292
+
Image,
293
+
ListView,
294
+
StyleSheet,
295
+
Text,
296
+
View,
297
+
} = React;
298
+
```
299
+
300
+
Now modify the render funtion so that once we have our data it renders a ListView of movies instead of a single movie.
301
+
302
+
```javascript
303
+
render:function() {
304
+
if (!this.state.loaded) {
305
+
returnthis.renderLoadingView();
306
+
}
307
+
308
+
return (
309
+
<ListView
310
+
dataSource={this.state.dataSource}
311
+
renderRow={this.renderMovie}
312
+
/>
313
+
);
314
+
},
315
+
```
316
+
317
+
What is this dataSource thing and why do we use it? This way we can very cheaply know which rows have changed between updates.
318
+
319
+
You'll notice we added dataSource from this.state. The next step is to add an empty dataSource to getInitialState. Also, now that we're storing the data in dataSource, we should change this.state.movies to be this.state.loaded (boolean) so we aren't storing the data twice.
320
+
321
+
```javascript
322
+
getInitialState:function() {
323
+
return {
324
+
dataSource:newListView.DataSource({
325
+
rowHasChanged: (row1, row2) => row1 !== row2,
326
+
}),
327
+
loaded:false,
328
+
};
329
+
},
330
+
```
331
+
332
+
And here's the modified this.setState in the response handler in fetchData:
0 commit comments