Fetching Newsfeed posts in the view

To use this API in the frontend, we will add a fetch method to client/post/api-post.js:

const listNewsFeed = (params, credentials) => {  return fetch('/api/posts/feed/'+ params.userId, {    method: 'GET',    headers: {      'Accept': 'application/json',      'Content-Type': 'application/json',      'Authorization': 'Bearer ' + credentials.t    }  }).then(response => {    return response.json()  }).catch((err) => console.log(err))}

This is the fetch method that will load the posts rendered in the PostList, which is added as a child component to the Newsfeed component. So this fetch needs to be called in the loadPosts method in the Newsfeed component.

mern-social/client/post/Newsfeed.js:

 loadPosts = () => { const jwt = auth.isAuthenticated() ...

Get Full-Stack React Projects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.