Skip to content

Instantly share code, notes, and snippets.

@mladenoff
Last active December 1, 2017 01:04
Show Gist options
  • Save mladenoff/d7db08dcabfa8721c5ab1a716e9754a1 to your computer and use it in GitHub Desktop.
Save mladenoff/d7db08dcabfa8721c5ab1a716e9754a1 to your computer and use it in GitHub Desktop.
An easy lodash selector for you!

This is a lightweight selector that can be used to match up something like your users (or playlists) slice of state (in this case an object) with your followees (the users you follow, in this case an array of userIds). Also works for something like photos or posts and your likedPosts/Photos array.

// fronted/reducers/selectors.js

import * as _ from 'lodash';

export const selectUsersByFollowees = (users, followees) => (
  _.filter(
    Object.values(users),
    user => (followees.includes(user.id)),
  )
);
// some sort of container
import { connect } from 'react-redux';
import {selectUsersByFollowees} from '../../reducers/selectors.js'

//...

const mapStateToProps = (state, ownProps) => ({
  followers: selectUsersByFollowees(state.entities.users, session.currentUser.followees),
});

//...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment