Skip to content

Commit 47146a4

Browse files
committed
feature: add fetch with abort controller
1 parent f09d70c commit 47146a4

File tree

9 files changed

+8550
-118
lines changed

9 files changed

+8550
-118
lines changed

.flowconfig

Lines changed: 0 additions & 69 deletions
This file was deleted.

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"arrowParens": "always",
3+
"singleQuote": true
4+
}

App.js

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,77 @@
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+
}
1839

19-
type Props = {};
20-
export default class App extends Component<Props> {
2140
render() {
41+
const { data, status, error } = this.state;
42+
2243
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>
2856
);
2957
}
3058
}
3159

3260
const styles = StyleSheet.create({
3361
container: {
3462
flex: 1,
35-
justifyContent: 'center',
36-
alignItems: 'center',
37-
backgroundColor: '#F5FCFF',
63+
// justifyContent: 'center',
64+
// alignItems: 'center',
65+
backgroundColor: '#F5FCFF'
3866
},
3967
welcome: {
4068
fontSize: 20,
4169
textAlign: 'center',
42-
margin: 10,
70+
margin: 10
4371
},
4472
instructions: {
4573
textAlign: 'center',
4674
color: '#333333',
47-
marginBottom: 5,
48-
},
75+
marginBottom: 5
76+
}
4977
});

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Roberto Achar
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# react-native-fetch-abort-controller
2+
3+
[![License][license-badge]][license-url]
4+
5+
> React Native + fetch + Abort Controller
6+
7+
# Development
8+
9+
- Clone the repo
10+
11+
```bash
12+
$ git clone https://github.com/robertoachar/react-native-fetch-abort-controller.git
13+
```
14+
15+
- Install dependencies
16+
17+
```bash
18+
$ npm install
19+
```
20+
21+
- Run scripts
22+
23+
| Action | Usage |
24+
| ---------------| ----------------- |
25+
| Run on Android | `npm run android` |
26+
| Run on iOS | `npm run ios` |
27+
28+
# Author
29+
30+
[Roberto Achar](https://twitter.com/robertoachar)
31+
32+
# License
33+
34+
[MIT](https://github.com/robertoachar/react-native-fetch-abort-controller/blob/master/LICENSE)
35+
36+
[license-badge]: https://img.shields.io/github/license/robertoachar/react-native-fetch-abort-controller.svg
37+
[license-url]: https://opensource.org/licenses/MIT

index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
/**
2-
* @format
3-
*/
4-
5-
import {AppRegistry} from 'react-native';
6-
import App from './App';
7-
import {name as appName} from './app.json';
1+
import { AppRegistry } from "react-native";
2+
import App from "./App";
3+
import { name as appName } from "./app.json";
84

95
AppRegistry.registerComponent(appName, () => App);

metro.config.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
/**
2-
* Metro configuration for React Native
3-
* https://github.com/facebook/react-native
4-
*
5-
* @format
6-
*/
7-
81
module.exports = {
92
transformer: {
103
getTransformOptions: async () => ({
114
transform: {
125
experimentalImportSupport: false,
13-
inlineRequires: false,
14-
},
15-
}),
16-
},
6+
inlineRequires: false
7+
}
8+
})
9+
}
1710
};

0 commit comments

Comments
 (0)