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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.