Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { StyleSheet, Text, View, Button, Alert, TextInput, Image, AsyncStorage, FlatList } from 'react-native';
  3.  
  4. export default class App extends React.Component {
  5.  
  6.   // Seperator styling code modified from this source: https://medium.com/react-native-development/how-to-use-the-flatlist-component-react-native-basics-92c482816fe6
  7.  
  8.   constructor(props)
  9.   {
  10.     super(props);
  11.     this.state = {data: [], search: ''}
  12.   }
  13.  
  14.   getData = () =>
  15.   {
  16.     const url = "http://www.recipepuppy.com/api/?i=" + this.state.search
  17.     fetch(url)
  18.     .then((response) => response.json())
  19.     .then((responseJson) =>
  20.     {
  21.       this.setState({
  22.         data: responseJson.results
  23.       })
  24.     })
  25.     .catch((error) =>
  26.     {
  27.         Alert.alert("Computer machine broke!")
  28.     })
  29.   }
  30.  
  31.   renderSeparator = () => {
  32.     return (
  33.         <View
  34.             style={{
  35.                 height: 1,
  36.                 width: "100%",
  37.                 backgroundColor: "black"
  38.             }}
  39.         />
  40.         );
  41.     };
  42.  
  43.   render() {
  44.     return (
  45.       <View style={styles.container}>
  46.         <FlatList
  47.           style={{marginTop: "10%", marginLeft: "25%", marginRight: "25%"}}
  48.           keyExtractor={item => item.href}
  49.           renderItem={({item}) => <View><Image source={{uri: item.thumbnail}} style={{width: 50, height: 50, borderColor: 'black'}}/><Text>{item.title}</Text></View>}
  50.           ItemSeparatorComponent={this.renderSeparator}
  51.           data={this.state.data}
  52.         />
  53.         <TextInput style={{width: 200, borderColor: 'grey', borderWidth: 1}} onChangeText={(search) => this.setState({search})}  value={this.state.search}/>
  54.         <Button onPress={this.getData} title="Search for Recipes"/>
  55.       </View>
  56.     );
  57.   }
  58. }
  59.  
  60. const styles = StyleSheet.create({
  61.   container: {
  62.     flex: 1,
  63.     backgroundColor: 'lightblue',
  64.     alignItems: 'center',
  65.     justifyContent: 'center',
  66.   },
  67.   thumbnails: {
  68.  
  69.   },
  70. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement