/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format * @flow */ 'use strict'; const React = require('react'); const ReactNative = require('react-native'); const {StyleSheet, Text, TextInput, View, Platform} = ReactNative; class XHRExampleFetch extends React.Component { responseURL: ?string; responseHeaders: ?Object; constructor(props: any) { super(props); this.state = { responseText: null, }; this.responseURL = null; this.responseHeaders = null; } submit(uri: string) { fetch(uri) .then(response => { this.responseURL = response.url; this.responseHeaders = response.headers; return response.text(); }) .then(body => { this.setState({responseText: body}); }); } _renderHeaders() { if (!this.responseHeaders) { return null; } const responseHeaders = []; const keys = Object.keys(this.responseHeaders.map); for (let i = 0; i < keys.length; i++) { const key = keys[i]; const value = this.responseHeaders.get(key); responseHeaders.push( {key}: {value} , ); } return responseHeaders; } render() { const responseURL = this.responseURL ? ( Server response URL: {this.responseURL} ) : null; const responseHeaders = this.responseHeaders ? ( Server response headers: {this._renderHeaders()} ) : null; const response = this.state.responseText ? ( Server response: ) : null; return ( Edit URL to submit: { this.submit(event.nativeEvent.text); }} style={styles.textInput} /> {responseURL} {responseHeaders} {response} ); } } const styles = StyleSheet.create({ textInput: { flex: 1, borderRadius: 3, borderColor: 'grey', borderWidth: 1, height: Platform.OS === 'android' ? 44 : 30, paddingLeft: 8, }, label: { flex: 1, color: '#aaa', fontWeight: '500', height: 20, }, textOutput: { flex: 1, fontSize: 17, borderRadius: 3, borderColor: 'grey', borderWidth: 1, height: 200, paddingLeft: 8, }, }); module.exports = XHRExampleFetch;