"// Call our database and ask for a subset of the user posts"

// screens/FeedScreen.jsimport firebase from 'firebase';import React, { Component } from 'react';import { LayoutAnimation, RefreshControl } from 'react-native';import List from '../components/List';import Fire from '../Fire';// Set the default number of images to load for each pagination.const PAGE_SIZE = 5;export default class FeedScreen extends Component {  state = {    loading: false,    posts: [],    data: {},  };  componentDidMount() {    // Check if we are signed in...    if (Fire.shared.uid) {      // If we are, then we can get the first 5 posts      this.makeRemoteRequest();    } else {      // If we aren't then we should just start observing changes. This will be called when the user signs in      firebase.auth().onAuthStateChanged(user => {        if (user) {          this.makeRemoteRequest();        }      });    }  }  // Append the item to our states `data` prop  addPosts = posts => {    this.setState(previousState => {      let data = {        ...previousState.data,        ...posts,      };      return {        data,        // Sort the data by timestamp        posts: Object.values(data).sort((a, b) => a.timestamp < b.timestamp),      };    });  };  // Call our database and ask for a subset of the user posts  makeRemoteRequest = async lastKey => {    // If we are currently getting posts, then bail out..    if (this.state.loading) {      return;    }    this.setState({ loading: true });    // The data prop will be an array of posts, the cursor will be used for pagination.    const { data, cursor } = await Fire.shared.getPaged({      size: PAGE_SIZE,      start: lastKey,    });    this.lastKnownKey = cursor;    // Iteratively add posts    let posts = {};    for (let child of data) {      posts[child.key] = child;    }    this.addPosts(posts);    // Finish loading, this will stop the refreshing animation.    this.setState({ loading: false });  };  // Because we want to get the most recent items, don't pass the cursor back.  // This will make the data base pull the most recent items.  _onRefresh = () => this.makeRemoteRequest();  // If we press the "Load More..." footer then get the next page of posts  onPressFooter = () => this.makeRemoteRequest(this.lastKnownKey);  render() {    // Let's make everything purrty by calling this method which animates layout changes.    LayoutAnimation.easeInEaseOut();    return (      <List        refreshControl={          <RefreshControl            refreshing={this.state.loading}            onRefresh={this._onRefresh}          />        }        onPressFooter={this.onPressFooter}        data={this.state.posts}      />    );  }}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source