Skip to content

Instantly share code, notes, and snippets.

@BretCameron
Created March 25, 2019 10:16
Show Gist options
  • Save BretCameron/b74b99bfc700978c8344da7631431830 to your computer and use it in GitHub Desktop.
Save BretCameron/b74b99bfc700978c8344da7631431830 to your computer and use it in GitHub Desktop.
The fetchPostData method in our Movies.js React component
fetchPostData() {
fetch(`http://localhost/yoursitename/wp-json/wp/v2/movies?per_page=100`)
.then(response => response.json())
.then(myJSON => {
let objLength = Object.keys(myJSON).length;
let newState = this.state;
for (let i = 0; i < objLength; i++) {
let objKey = Object.values(myJSON)[i].title.rendered;
let currentMovie = newState.data[objKey];
currentMovie = {};
currentMovie.name = Object.values(myJSON)[i].title.rendered;
currentMovie.description = Object.values(myJSON)[i].content.rendered;
currentMovie.featured_image = Object.values(myJSON)[i]['featured_image_url'];
currentMovie.genre = Object.values(myJSON)[i]['genre'];
}
this.setState(newState);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment