Created
March 25, 2019 10:16
-
-
Save BretCameron/b74b99bfc700978c8344da7631431830 to your computer and use it in GitHub Desktop.
The fetchPostData method in our Movies.js React component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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