[Question] Is this the way I should reselect an article and the articles?
Let's take for instance this code
const visibleIDsListSelector = (state) => state.companyArticles.visibleIDs.IDs;
const byIDsMapSelector = (state) => state.companyArticles.byID;
export const getArticle = createSelector(
byIDsMapSelector,
(theMap) => (id) => theMap.get(String(id))
);
export const getVisibleArticles = createSelector(
visibleIDsListSelector,
getArticle,
(IDs : any, articleSelector) => IDs.map((id) => articleSelector(id))
);
So, I want to return 2 functions which:
getArticlereturns the article by idgetVisibleArticlesreturns all the visible articles (using their IDs)
Do I do it in the right way? I feel that by passing getArticle as a function and then using it inside the getVisibleArticles I feel is not the proper way.
Note:
state.companyArticles.visibleIDs.IDs is a ImmutableJS.List
state.companyArticles.byID is a ImmutableJS.Map
I've only looked at it quickly, but I'm not sure using getArticle as an input selector to getVisibleArticles gives you any benefit. Remember that createSelector only memoizes its last call, so mapping articleSelector across a list is going to result in a cache miss each time.
Does something like this (not tested) will work for you?
const visibleIDsListSelector = (state) => state.companyArticles.visibleIDs.IDs;
const byIDsMapSelector = (state) => state.companyArticles.byID;
export const getArticle = (state, id) => byIDsMapSelector(state).get(id);
export const getVisibleArticles = createSelector(
visibleIDsListSelector,
byIDsMapSelector,
(IDList : any, IDMap) => IDList.map(id => IDMap.get(String(id)))
);
Nice. Let me check
Great answer :) Thanks