Advertisement
Guest User

How to store API response in memory

a guest
Apr 20th, 2018
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. export default class Category extends React.Component {
  2.     constructor(props) {
  3.         super(props);
  4.         this.state = {
  5.             isLoading: false,
  6.             categories: [],
  7.             loaded:false,
  8.             page: 1,
  9.         };
  10.     }
  11.  
  12.     static navigationOptions = {
  13.         headerTitle: 'Categories',
  14.         title: 'Categories',
  15.     };
  16.  
  17.     componentDidMount() {
  18.         this.fetchCategories();
  19.     }
  20.  
  21.     fetchCategories() {
  22.         this.setState({isLoading: true});
  23.         categoryApi.getAll(this.state.page).then(response => {
  24.             if (response.data) {
  25.                 console.log(response.data);
  26.                 this.setState({
  27.                     categories: this.state.categories.concat(response.data),
  28.                     isLoading: false,
  29.                 });
  30.             } else {
  31.                 this.setState({isLoading: false});
  32.                 console.log(response);
  33.                 Alert.alert(
  34.                     constants.app.errorTitle,
  35.                     constants.app.errorMessage,
  36.                     [],
  37.                     {cancelable: true}
  38.                 )
  39.             }
  40.         });
  41.     }
  42.  
  43.     componentWillMount() {
  44.  
  45.     }
  46.  
  47.     render() {
  48.         const {navigate} = this.props.navigation;
  49.  
  50.         return (
  51.             <View style={styles.container}>
  52.                 <View style={styles.projektiHeader}>
  53.                     <Text style={styles.projekti}>VALITSE PROJEKTI</Text>
  54.                 </View>
  55.                 <View style={styles.categoriesList}>
  56.                     <FlatList
  57.                         data={this.state.categories}
  58.                         extraData={this.state}
  59.                         renderItem={(rowData) => <CategoryItem navigate={navigate} item={rowData}/>}
  60.                         onEndReached={this.showMore}
  61.                         keyExtractor={(item, index) => index}
  62.                         // onEndReachedThreshold={0.5}
  63.                     />
  64.                 </View>
  65.                 <View style={styles.shopsNear}>
  66.                     <ShopsNear navigate={navigate}/>
  67.                 </View>
  68.             </View>
  69.         );
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement