title: How to Cache API data using AsyncStorage React Native
body: <p>Fairly new to React native and its concepts. I have been playing with RN for a while to create an application to fetch API data from </p>
  
  <p><em>http://jsonplaceholder.typicode.com/photos</em></p>
  
  <p>I have been looking into the documentation of AsyncStorage to implement how i can cache the API data so that upon terminating the application, it doesn't have to deal with fetching the data from web again and again, but wasn't successfully able to implement it. </p>
  
  <p>It will be great if you can provide me help/suggestion based on it. I have included my source code for the 2 important files in my application, along with the a <em>Test.js</em> file with how i was trying to achieve. </p>
  
  <pre><code>import React, {Component} from 'react';
  import { FlatList, View, Text, AsyncStorage, ActivityIndicator } from 'react-native';
  import axios from 'axios';
  import GalleryDetail from './GalleryDetail';
  
  class GalleryList extends Component {
  
  state = { photos: []};
  
  componentDidMount() {
      axios.get('http://jsonplaceholder.typicode.com/photos')
      .then(response =&gt; this.setState({ photos: response.data }))
      .catch((error)=&gt; console.warn("fetch Error: ", error));
  }
  
  getPhotos = async()=&gt; {
      try {
          photos = await AsyncStorage.getItem('GalleryPhotos');
      }
      catch (error) {
          console.error(error);
      }
  }
  
  savePhotos(){
      AsyncStorage.setItem('GalleryPhotos', this.state.photos);
      console.log('works !');
  }
  
  renderPhoto = ({item})=&gt; {
      return &lt;GalleryDetail photo={item}/&gt;
  }
  
  keyExtractor = (photo, index) =&gt; photo.id;
  
  render () {
  
      if(!this.state.photos){
          return &lt;ActivityIndicator/&gt;;
      }
  
      return (
              &lt;FlatList
                  data = {this.state.photos}
                  keyExtractor={this.keyExtractor}
                  renderItem={this.renderPhoto}
              /&gt;
      );
  }
  }
  
  export default GalleryList;
  </code></pre>
  
  <p>and GalleryDetail linked with GalleryList- </p>
  
  <pre><code>import React, {Component} from 'react';
  import { Text, View, Image } from 'react-native';
   import Card from './Card';
   import CardSection from './CardSection';
  
  const GalleryDetail = (props)=&gt; {
       return (
          &lt;Card&gt;
              &lt;CardSection style = {styles.headerContentStyle}&gt;
                  &lt;Image
                      style={styles.thumbnailStyle}
                      source = {{ uri: props.photo.thumbnailUrl}}/&gt;
                  &lt;Text style= {styles.textStyle}&gt;{props.photo.title}    &lt;/Text&gt;
              &lt;/CardSection&gt;
          &lt;/Card&gt;
      );
  };
  
  const styles = {
     headerContentStyle: {
         flexDirection: 'column',
         justifyContent: 'space-around'
     },
  
     thumbnailStyle: {
         height: 60,
         width: 60
     },
  
     textStyle: {
         fontSize: 12,
         //textAlign: 'right',
         flexDirection: 'row',
         justifyContent: 'flex-end',
         flex: 1,
         flexWrap: 'wrap',
         marginLeft: 5,
         marginRight: 5,
      }
      }
  
      export default GalleryDetail;
  </code></pre>
  
  <p>My method of trying was that-
  Upon launching the application, it will first look in asyncStorage, if it finds the data- it fetches from async otherwise going to the web,fetching and storing again for later use. 
  I tried to implement somewhat like this in a separate file since i dint wanted to breakdown my already running app. The weird broken syntax is</p>
  
  <pre><code>State = {
          photos: []
      }
  
      componentDidMount() {
  
          // just a variable acting to fetch data from the stored keyvalue pair
  
          check = AsyncStorage.getItem("PhotosKey").then((response) =&gt; {
                       this.setState({"PhotosKey": response});
                        }).done();
  
          if(check) {
              console.log('Data was fetched!!!!!');
              check();
          }
  
          else {
              console.log("Data was not fetched!");
  
              var Data = axios.get('http://jsonplaceholder.typicode.com/photos').
              then(response =&gt; this.setState({ photos: response.data })).
              catch((error)=&gt; console.warn("fetch Error: ", error));
  
  
  
          }
      }
  </code></pre>
  
  <p>Thanks in advance!</p>