|
| 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-native'); |
| 19 | +var { |
| 20 | + StyleSheet, |
| 21 | + Text, |
| 22 | + TextInput, |
| 23 | + View, |
| 24 | + Platform, |
| 25 | +} = React; |
| 26 | + |
| 27 | + |
| 28 | +class XHRExampleFetch extends React.Component { |
| 29 | + |
| 30 | + constructor(props: any) { |
| 31 | + super(props); |
| 32 | + this.state = { |
| 33 | + responseText: null, |
| 34 | + }; |
| 35 | + this.responseURL = null; |
| 36 | + } |
| 37 | + |
| 38 | + submit(uri: String) { |
| 39 | + fetch(uri).then((response) => { |
| 40 | + this.responseURL = response.url; |
| 41 | + return response.text(); |
| 42 | + }).then((body) => { |
| 43 | + this.setState({responseText: body}); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + render() { |
| 48 | + |
| 49 | + var responseURL = this.responseURL ? ( |
| 50 | + <View style={{marginTop: 10}}> |
| 51 | + <Text style={styles.label}>Server response URL:</Text> |
| 52 | + <Text>{this.responseURL}</Text> |
| 53 | + </View> |
| 54 | + ) : null; |
| 55 | + |
| 56 | + var response = this.state.responseText ? ( |
| 57 | + <View style={{marginTop: 10}}> |
| 58 | + <Text style={styles.label}>Server response:</Text> |
| 59 | + <TextInput |
| 60 | + editable={false} |
| 61 | + multiline={true} |
| 62 | + defaultValue={this.state.responseText} |
| 63 | + style={styles.textOutput} |
| 64 | + /> |
| 65 | + </View> |
| 66 | + ) : null; |
| 67 | + |
| 68 | + return ( |
| 69 | + <View> |
| 70 | + <Text style={styles.label}>Edit URL to submit:</Text> |
| 71 | + <TextInput |
| 72 | + returnKeyType="go" |
| 73 | + defaultValue="http://www.posttestserver.com/post.php" |
| 74 | + onSubmitEditing={(event)=> { |
| 75 | + this.submit(event.nativeEvent.text); |
| 76 | + }} |
| 77 | + style={styles.textInput} |
| 78 | + /> |
| 79 | + {responseURL} |
| 80 | + {response} |
| 81 | + </View> |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +var styles = StyleSheet.create({ |
| 87 | + textInput: { |
| 88 | + flex: 1, |
| 89 | + borderRadius: 3, |
| 90 | + borderColor: 'grey', |
| 91 | + borderWidth: 1, |
| 92 | + height: Platform.OS === 'android' ? 44 : 30, |
| 93 | + paddingLeft: 8, |
| 94 | + }, |
| 95 | + label: { |
| 96 | + flex: 1, |
| 97 | + color: '#aaa', |
| 98 | + fontWeight: '500', |
| 99 | + height: 20, |
| 100 | + }, |
| 101 | + textOutput: { |
| 102 | + flex: 1, |
| 103 | + fontSize: 17, |
| 104 | + borderRadius: 3, |
| 105 | + borderColor: 'grey', |
| 106 | + borderWidth: 1, |
| 107 | + height: 200, |
| 108 | + paddingLeft: 8, |
| 109 | + }, |
| 110 | +}); |
| 111 | + |
| 112 | +module.exports = XHRExampleFetch; |
0 commit comments