|
| 1 | +/** |
| 2 | + * The examples provided by Facebook are for non-commercial testing and |
| 3 | + * evaluation purposes only. |
| 4 | + * |
| 5 | + * Facebook reserves all rights not expressly granted. |
| 6 | + * |
| 7 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 8 | + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 9 | + * FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL |
| 10 | + * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
| 11 | + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 12 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 13 | + * |
| 14 | + * @flow |
| 15 | + */ |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +var React = require('react'); |
| 19 | +var ReactNative = require('react-native'); |
| 20 | +var { |
| 21 | + StyleSheet, |
| 22 | + Text, |
| 23 | + TouchableHighlight, |
| 24 | + View, |
| 25 | +} = ReactNative; |
| 26 | + |
| 27 | +class XHRExampleOnTimeOut extends React.Component { |
| 28 | + state: any; |
| 29 | + xhr: XMLHttpRequest; |
| 30 | + |
| 31 | + constructor(props: any) { |
| 32 | + super(props); |
| 33 | + this.state = { |
| 34 | + status: '', |
| 35 | + loading: false |
| 36 | + }; |
| 37 | + } |
| 38 | + |
| 39 | + loadTimeOutRequest() { |
| 40 | + this.xhr && this.xhr.abort(); |
| 41 | + |
| 42 | + var xhr = this.xhr || new XMLHttpRequest(); |
| 43 | + |
| 44 | + xhr.onerror = ()=> { |
| 45 | + console.log('Status ', xhr.status); |
| 46 | + console.log('Error ', xhr.responseText); |
| 47 | + }; |
| 48 | + |
| 49 | + xhr.ontimeout = () => { |
| 50 | + this.setState({ |
| 51 | + status: xhr.responseText, |
| 52 | + loading: false |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + xhr.onload = () => { |
| 57 | + console.log('Status ', xhr.status); |
| 58 | + console.log('Response ', xhr.responseText); |
| 59 | + }; |
| 60 | + |
| 61 | + xhr.open('GET', 'https://httpbin.org/delay/5'); // request to take 5 seconds to load |
| 62 | + xhr.timeout = 2000; // request times out in 2 seconds |
| 63 | + xhr.send(); |
| 64 | + this.xhr = xhr; |
| 65 | + |
| 66 | + this.setState({loading: true}); |
| 67 | + } |
| 68 | + |
| 69 | + componentWillUnmount() { |
| 70 | + this.xhr && this.xhr.abort(); |
| 71 | + } |
| 72 | + |
| 73 | + render() { |
| 74 | + var button = this.state.loading ? ( |
| 75 | + <View style={styles.wrapper}> |
| 76 | + <View style={styles.button}> |
| 77 | + <Text>Loading...</Text> |
| 78 | + </View> |
| 79 | + </View> |
| 80 | + ) : ( |
| 81 | + <TouchableHighlight |
| 82 | + style={styles.wrapper} |
| 83 | + onPress={this.loadTimeOutRequest.bind(this)}> |
| 84 | + <View style={styles.button}> |
| 85 | + <Text>Make Time Out Request</Text> |
| 86 | + </View> |
| 87 | + </TouchableHighlight> |
| 88 | + ); |
| 89 | + |
| 90 | + return ( |
| 91 | + <View> |
| 92 | + {button} |
| 93 | + <Text>{this.state.status}</Text> |
| 94 | + </View> |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +var styles = StyleSheet.create({ |
| 100 | + wrapper: { |
| 101 | + borderRadius: 5, |
| 102 | + marginBottom: 5, |
| 103 | + }, |
| 104 | + button: { |
| 105 | + backgroundColor: '#eeeeee', |
| 106 | + padding: 8, |
| 107 | + }, |
| 108 | +}); |
| 109 | + |
| 110 | +module.exports = XHRExampleOnTimeOut; |
0 commit comments