Last active
February 9, 2020 03:56
-
-
Save json2d/e7f65204cf271063cc7e29dd5f14a0b7 to your computer and use it in GitHub Desktop.
proposal to put state into callback scope through 'this'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 */ ) ) | |
} | |
) |
...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
heres a way (with its own tradeoffs) achieving the same result w/o having a
state
argit parametizes
ids
(previouslyrecentIds
) outside ofcreateSelector
, and with a little bit more finesse its able to usecreateSelector
as intended to memoizenow the kinda ugly part
what we need to do is take the result of
recentIdsSelector
and use it as theids
arg inentriesSelector
.to do this we'll jump into
react-redux
land (where we actually use our reselectors) and use 2 levels ofconnect
, one around the other.this might just be the best way, as there seems to be no way to write the
recentSelector
(see prev comments) as a single reselectorconnect
acts like the glue for our new selector code, giving us the desired high-level behavior:recentEntries
will only be recomputed if:state.recentIds
changesstate.entryById
with one of those ids changesin general this is how you could use result of one reselector as the arg of another reselector without breaking memoization that
reselect
provides