Now that we have an action creator to pull a single user from the API, we just need to modify our fetchPosts action creator to call the fetchUser action creator for every username:
- We start by creating a new function, which will pull all usernames from an array of posts. Edit src/actions/posts.js and create a new function:
const getUsernamesFromPosts = (posts) =>
- Here, we will make use of .reduce() to end up with an array of usernames (without duplicates):
posts.reduce((usernames, post) => { if (!usernames.includes(post.user)) { return [ ...usernames, post.user ] }