Fetching users when fetching posts

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:

  1. 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) =>
Note that we do not export this function because it's not an action creator. It is simply a helper function for other action creators.
  1. 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 ]    }

Get Learning Redux 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.