@@ -182,44 +182,48 @@ in [Editable Complex Components](#editable-complex-components).
182182![ demo-3] ( ./.github/large-network-example.gif )
183183
184184``` jsx
185- state = {
186- loading: true ,
187- users: [],
188- }
189-
190- async componentWillMount () {
191- const response = await fetch (' https://randomuser.me/api/?results=5000' )
192- const data = await response .json ()
193-
194- this .setState ({
195- loading: false ,
196- users: data .results .map (a => ({
197- name: ` ${ a .name .first } ${ a .name .last } ` ,
198- id: a .registered ,
199- })),
200- })
201- }
185+ () => {
186+ const [loading , setLoading ] = useState (true );
187+ const [users , setUsers ] = useState ([]);
188+
189+ useEffect (() => {
190+ const getUsers = async () => {
191+ const response = await fetch (' https://randomuser.me/api/?results=5000' );
192+ const data = await response .json ();
193+
194+ setLoading (false );
195+ setUsers (
196+ data .results .map (a => ({
197+ name: ` ${ a .name .first } ${ a .name .last } ` ,
198+ id: a .registered ,
199+ }))
200+ );
201+ };
202+
203+ getUsers ();
204+ }, []);
202205
203- render () {
204206 return (
205207 < View style= {{ flex: 1 }}>
206208 < Text style= {styles .title }>
207- {this . state . loading ? ' Fetching' : ' Fetched' } 5000 users
209+ {loading ? ' Fetching' : ' Fetched' } 5000 users
208210 < / Text >
209211
210- {this . state . loading && < ActivityIndicator / > }
212+ {loading && < ActivityIndicator / > }
211213
212214 < TableView
213215 style= {{ flex: 1 }}
214216 tableViewCellStyle= {TableView .Consts .CellStyle .Subtitle }
215217 >
216218 < Section>
217- {this .state .users .map (a => < Item key= {a .id }> {a .name }< / Item> )}
219+ {users .map (a => (
220+ < Item key= {a .id }> {a .name }< / Item>
221+ ))}
218222 < / Section>
219223 < / TableView>
220224 < / View>
221- )
222- }
225+ );
226+ };
223227```
224228
225229### App-bundled JSON with filter and selected value checkmarked
@@ -256,7 +260,7 @@ render() {
256260 < View style= {{ flex: 1 }}>
257261 < TableView
258262 style= {{ flex: 1 }}
259- editing= {this . props . navigation .state . params . editing }
263+ editing= {navigation .getParam ( ' editing' ) }
260264 >
261265 < Section canMove canEdit>
262266 < Item canEdit= {false }> Item 1 < / Item>
@@ -279,59 +283,84 @@ render() {
279283![ pull to refresh example] ( ./.github/pull-to-refresh-example.gif )
280284
281285``` jsx
282- state = {
283- loading: true ,
284- users: [],
285- refreshing: false ,
286- amount: 10 ,
286+ function reducer (state , action ) {
287+ switch (action .type ) {
288+ case ' getUsers' :
289+ return { ... state, loading: false , users: action .payload };
290+ case ' startRefresh' :
291+ return { ... state, refreshing: true };
292+ case ' endRefresh' :
293+ return {
294+ ... state,
295+ refreshing: false ,
296+ amount: state .amount + 10 ,
297+ users: [... state .users , ... action .payload ],
298+ };
299+ default :
300+ return state;
301+ }
287302}
288303
289- async componentWillMount () {
290- const users = await this .fetchUsers ()
304+ () => {
305+ const [{ loading , amount , refreshing , users }, dispatch ] = useReducer (
306+ reducer,
307+ {
308+ loading: true ,
309+ users: [],
310+ refreshing: false ,
311+ amount: 10 ,
312+ }
313+ );
291314
292- this . setState ( {
293- loading : false ,
294- users,
295- })
296- }
315+ useEffect (() => {
316+ const getUsers = async () => {
317+ const data = await fetchUsers ();
318+ dispatch ({ type : ' getUsers ' , payload : data });
319+ };
297320
298- fetchUsers = async () => {
299- const response = await fetch (' https://randomuser.me/api/?results=10' )
300- const data = await response .json ()
321+ getUsers ();
322+ }, []);
301323
302- return data .results .map (a => ({
303- name: ` ${ a .name .first } ${ a .name .last } ` ,
304- id: a .registered ,
305- }))
306- }
324+ const fetchUsers = async () => {
325+ const response = await fetch (' https://randomuser.me/api/?results=10' );
326+ const data = await response .json ();
307327
308- fetchMore = () => {
309- this .setState ({ refreshing: true }, async () => {
310- const users = await this .fetchUsers ()
311- this .setState ({ users: [... users, ... this .state .users ], refreshing: false , amount: this .state .amount + 10 })
312- })
313- }
328+ return data .results .map (a => ({
329+ name: ` ${ a .name .first } ${ a .name .last } ` ,
330+ id: a .login .uuid ,
331+ }));
332+ };
333+
334+ const fetchMore = async () => {
335+ dispatch ({ type: ' startRefresh' });
336+ const data = await fetchUsers ();
337+ dispatch ({ type: ' endRefresh' , payload: data });
338+ };
314339
315- render () {
316340 return (
317341 < View style= {{ flex: 1 }}>
318342 < Text style= {styles .title }>
319- {this . state . loading ? ' Fetching' : ' Fetched' } {this . state . amount } users
343+ {loading ? ' Fetching' : ' Fetched' } {amount} users
320344 < / Text >
321345
322- {this . state . loading && < ActivityIndicator / > }
346+ {loading && < ActivityIndicator / > }
323347
324348 < TableView
325349 style= {{ flex: 1 }}
326350 tableViewCellStyle= {TableView .Consts .CellStyle .Subtitle }
327351 canRefresh
328- refreshing= {this . state . refreshing }
329- onRefresh= {this . fetchMore }
352+ refreshing= {refreshing}
353+ onRefresh= {fetchMore}
330354 >
331- < Section> {this .state .users .map (a => < Item key= {a .id }> {a .name }< / Item> )}< / Section>
355+ < Section>
356+ {users .map (a => (
357+ < Item key= {a .id }> {a .name }< / Item>
358+ ))}
359+ < / Section>
332360 < / TableView>
333361 < / View>
334- )
362+ );
363+ };
335364}
336365```
337366
@@ -395,14 +424,14 @@ An `image` prop can be a string pointing to the name of an asset in your "Asset
395424Catalog". In this case an ` imageWidth ` prop is recommended.
396425
397426``` jsx
398- < Item image= " icon-success.png" imageWidth= {40 } / > ;
427+ < Item image= " icon-success.png" imageWidth= {40 } / >
399428```
400429
401430Alternatively, you can ` require ` the image from your local app code. In this case
402431an ` imageWidth ` is unnecessary.
403432
404433``` jsx
405- < Item image= {require (' ../images/icon-success.png' )} / > ;
434+ < Item image= {require (' ../images/icon-success.png' )} / >
406435```
407436
408437### Editable Complex Components
@@ -492,7 +521,7 @@ For more examples, see examples/TableViewDemo.
492521 / >
493522 );
494523 })}
495- < / Section> ;
524+ < / Section>
496525```
497526
498527Note that the props you pass must be primitive types: they cannot be objects.
0 commit comments