O'Reilly logo

Learning Redux by Daniel Bugl

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ]    }

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required