|
1 | | -/** |
2 | | - * Sample React Native App |
3 | | - * https://github.com/facebook/react-native |
4 | | - * |
5 | | - * @format |
6 | | - * @flow |
7 | | - */ |
8 | | - |
9 | | -import React, {Component} from 'react'; |
10 | | -import {Platform, StyleSheet, Text, View} from 'react-native'; |
11 | | - |
12 | | -const instructions = Platform.select({ |
13 | | - ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu', |
14 | | - android: |
15 | | - 'Double tap R on your keyboard to reload,\n' + |
16 | | - 'Shake or press menu button for dev menu', |
17 | | -}); |
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { Button, StyleSheet, Text, ScrollView } from 'react-native'; |
| 3 | +import 'abortcontroller-polyfill'; |
| 4 | + |
| 5 | +export default class App extends Component { |
| 6 | + constructor(props) { |
| 7 | + super(props); |
| 8 | + |
| 9 | + this.state = { |
| 10 | + status: null, |
| 11 | + data: null, |
| 12 | + error: null |
| 13 | + }; |
| 14 | + |
| 15 | + this.handleFetch = this.handleFetch.bind(this); |
| 16 | + this.handleAbort = this.handleAbort.bind(this); |
| 17 | + } |
| 18 | + |
| 19 | + handleFetch() { |
| 20 | + if (!AbortController) { |
| 21 | + return this.setState({ status: 'Not implemented' }); |
| 22 | + } |
| 23 | + |
| 24 | + this.controller = new window.AbortController(); |
| 25 | + this.signal = this.controller.signal; |
| 26 | + |
| 27 | + this.setState({ status: 'loading...' }); |
| 28 | + |
| 29 | + fetch('https://swapi.co/api/people/1', { signal: this.signal }) |
| 30 | + .then((response) => response.json()) |
| 31 | + .then((data) => this.setState({ status: 'loaded', error: null, data })) |
| 32 | + .catch((error) => this.setState({ error: error.name })); |
| 33 | + } |
| 34 | + |
| 35 | + handleAbort() { |
| 36 | + this.controller.abort(); |
| 37 | + this.setState({ status: 'aborted' }); |
| 38 | + } |
18 | 39 |
|
19 | | -type Props = {}; |
20 | | -export default class App extends Component<Props> { |
21 | 40 | render() { |
| 41 | + const { data, status, error } = this.state; |
| 42 | + |
22 | 43 | return ( |
23 | | - <View style={styles.container}> |
24 | | - <Text style={styles.welcome}>Welcome to React Native!</Text> |
25 | | - <Text style={styles.instructions}>To get started, edit App.js</Text> |
26 | | - <Text style={styles.instructions}>{instructions}</Text> |
27 | | - </View> |
| 44 | + <ScrollView style={styles.container}> |
| 45 | + <Text style={styles.welcome}> |
| 46 | + React Native + fetch + Abort Controller |
| 47 | + </Text> |
| 48 | + |
| 49 | + <Button title="Fetch data" onPress={this.handleFetch} /> |
| 50 | + <Button title="Abort" onPress={this.handleAbort} /> |
| 51 | + <Text style={styles.instructions}>{`Status: ${status}`}</Text> |
| 52 | + <Text style={styles.instructions}>{`Error: ${error}`}</Text> |
| 53 | + <Text style={styles.instructions}>Data</Text> |
| 54 | + <Text style={styles.instructions}>{JSON.stringify(data, null, 2)}</Text> |
| 55 | + </ScrollView> |
28 | 56 | ); |
29 | 57 | } |
30 | 58 | } |
31 | 59 |
|
32 | 60 | const styles = StyleSheet.create({ |
33 | 61 | container: { |
34 | 62 | flex: 1, |
35 | | - justifyContent: 'center', |
36 | | - alignItems: 'center', |
37 | | - backgroundColor: '#F5FCFF', |
| 63 | + // justifyContent: 'center', |
| 64 | + // alignItems: 'center', |
| 65 | + backgroundColor: '#F5FCFF' |
38 | 66 | }, |
39 | 67 | welcome: { |
40 | 68 | fontSize: 20, |
41 | 69 | textAlign: 'center', |
42 | | - margin: 10, |
| 70 | + margin: 10 |
43 | 71 | }, |
44 | 72 | instructions: { |
45 | 73 | textAlign: 'center', |
46 | 74 | color: '#333333', |
47 | | - marginBottom: 5, |
48 | | - }, |
| 75 | + marginBottom: 5 |
| 76 | + } |
49 | 77 | }); |
0 commit comments