Skip to content

Instantly share code, notes, and snippets.

@json2d
Last active February 9, 2020 03:56
Show Gist options
  • Save json2d/e7f65204cf271063cc7e29dd5f14a0b7 to your computer and use it in GitHub Desktop.
Save json2d/e7f65204cf271063cc7e29dd5f14a0b7 to your computer and use it in GitHub Desktop.
proposal to put state into callback scope through 'this'
const entryByIdSelector = (state) => state.todos.entrybyId
const recentIdsSelector = (state) => state.todos.recentIds
const entrySelector = _.memoize(
(id) => createSelector(
entryByIdSelector,
(entryById) => entryById[id]
)
)
const recentSelector = createSelector(
recentIdsSelector,
function (ids) {
return ids.map( id => entrySelector(id)(this.state /* magic */ ) )
}
)
@json2d
Copy link
Author

json2d commented Jun 29, 2019

...or just deal with a potential performance hit when doing it all in one connect 🥇

const mstp = (state, props) => { 
  const recentIds = recentIdsSelector(state)
  const recentEntries = entriesSelector(recentIds)(state)
  
  return { recentEntries }
}

let RecentContainer = connect(mstp)(Recent)

same high-level behavior

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