Skip to content

Instantly share code, notes, and snippets.

@rocaiguina
Created April 20, 2020 23:41
Show Gist options
  • Save rocaiguina/a9b90d7b8afcdd2a2e9b116ad82daa39 to your computer and use it in GitHub Desktop.
Save rocaiguina/a9b90d7b8afcdd2a2e9b116ad82daa39 to your computer and use it in GitHub Desktop.
import React, {Component} from 'react';
import {View} from 'react-native';
import PostCollection from '../components/PostCollection';
export default class Home extends Component {
constructor(props) {
super(props);
this.state = {
posts: [],
page: 1,
loading: true,
refreshing: false,
};
this.getPosts = this.getPosts.bind(this);
this.loadMore = this.loadMore.bind(this);
}
componentDidMount() {
this.getPosts();
}
getPosts() {
const {page} = this.state;
fetch(
`http://jayfonseca.mrddevelopment.com/wp-json/wp/v2/posts?&page=${page}&per_page=5&state=publish&_embed`,
)
.then((response) => {
if (!response.ok) {
return Promise.reject(response);
}
return response.json();
})
.then((response) => {
this.setState({
posts: response,
loading: false,
page: page + 1,
// refreshing: false,
});
})
.catch((err) =>
Promise.reject(
JSON.stringify({
status: 'error',
message: err.message || '',
}),
),
);
}
loadMore() {
const {page, posts} = this.state;
fetch(
`http://jayfonseca.mrddevelopment.com/wp-json/wp/v2/posts?&page=${page}&per_page=5&state=publish&_embed`,
)
.then((response) => response.json())
.then((response) => {
// if (response.status === 200) {
// // this.setState({
// // posts: posts.concat(response),
// // page: this.state.page + 1,
// // loading: false,
// // });
// console.log(response);
// }
console.log(response);
return;
})
.catch((error) => {
console.error(error);
});
}
render() {
const {posts, loading} = this.state;
console.log(posts);
return (
<View
style={{
backgroundColor: '#fff',
padding: 8,
flex: 1,
}}>
<PostCollection
posts={posts}
loading={loading}
loadMore={this.loadMore}
/>
</View>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment