Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
### Setup
* Fork, clone, yarn install, yarn start
*- Fork, clone, yarn install, yarn start
### Let's fetch users
* Go to this URL in your browser, https://jsonplaceholder.typicode.com/users
* Study the JSON format to understand what type of data it returns
* Now you need your running code to be able to retrieve this data so that you can use it to make a webpage.
* In index.js, make a fetch call to the url
* Specify the callback function usersRetrieved to handle when the fetch call is done, .then()
* Specify the callback function userJSONReady to handle when the json is ready, .then()
*- Go to this URL in your browser, https://jsonplaceholder.typicode.com/users
*- Study the JSON format to understand what type of data it returns
*- Now you need your running code to be able to retrieve this data so that you can use it to make a webpage.
*- In index.js, make a fetch call to the url
*- Specify the callback function usersRetrieved to handle when the fetch call is done, .then()
*- Specify the callback function userJSONReady to handle when the json is ready, .then()
### Let's fetch comments
* Go to this URL in your browser, https://jsonplaceholder.typicode.com/comments
* Study the JSON format to understand what type of data it returns
* In index.js, make a fetch call to the url
* Try using inline functions as the callbacks to the .then()
* Use usersRetreived as an example of how to put the data into the webpage
* Loop through the comments array and create a div for each one that shows the comment name.
* Set the innerHTML of a div with id = comments
*- Go to this URL in your browser, https://jsonplaceholder.typicode.com/comments
*- Study the JSON format to understand what type of data it returns
*- In index.js, make a fetch call to the url
*- Try using inline functions as the callbacks to the .then()
*- Use usersRetreived as an example of how to put the data into the webpage
*- Loop through the comments array and create a div for each one that shows the comment name.
*- Set the innerHTML of a div with id = comments
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<body>
<div id="root"></div>
<div id="users"></div>
<hr>
<div id="comments"></div>
</body>
</html>
17 changes: 17 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,20 @@ function userJSONReady(users) {
usersDiv.innerHTML = (usersHTML);
}

fetch('https://jsonplaceholder.typicode.com/users') //fetch users
.then(usersRetrieved)
.then(userJSONReady);

function commentJSONReady(comments) {
const commentsDiv = document.getElementById('comments');
let commentsHTML = '';
for (let i = 0; i < comments.length; i++) {
const comment = comments[i];
commentsHTML += `<div> ${comment.name} </div>`;
}
commentsDiv.innerHTML = commentsHTML;
}

fetch('https://jsonplaceholder.typicode.com/comments') //fetch comments
.then(res => res.json())
.then(commentJSONReady);