Question about memorized Multiple Components
I didn't go into the code yet, but just being curious, from the people who understand the internals
const getVisibleTodos = makeGetVisibleTodos()
const mapStateToProps = (state, props) => {
return {
todos: getVisibleTodos(state, props)
}
}
return mapStateToProps
}
if it's being refactored to the following, is it gonna work as well?
const getVisibilityFilter = listId => state =>
state.todoLists[listId].visibilityFilter
const getTodos = listId => state =>
state.todoLists[listId].todos
const makeGetVisibleTodos = (listId) => {
return createSelector(
[ getVisibilityFilter(listId), getTodos(listId) ],
(visibilityFilter, todos) => {
switch (visibilityFilter) {
case 'SHOW_COMPLETED':
return todos.filter(todo => todo.completed)
case 'SHOW_ACTIVE':
return todos.filter(todo => !todo.completed)
default:
return todos
}
}
)
}
.....
const mapStateToProps = (state, props) => {
return {
todos: makeGetVisibleTodos(props.listId)(state, props)
}
}
....
I was wondering about something like this too to potentially decouple the selector from the component.
I was about to ask similar, is it wise to approach list of todo item with multiple other attributes as well. For e.g. let us take a use case a trello but still more functionalities like, visibility filter is like (colors:["red", "purple","yellow"], hasAttachments: true, members: ["a", "b", "c"], startDate> today(), endDate < nextWeek() etc...? create a separate createSelectors for each? and when all attributes set to null, show all items with separate selector?, I am not sure how the internals of selectors and immutableJS work... appreciate if you let me know what would be the best approach for such a kind of use case
@RayDaiMYOB, I've not tested your code, but I don't see why it shouldn't work.
Since they are pure functions, Reselect factories can accept arguments, and you have transformed both getVisibilityFilter and getTodos into 2 selector factories which accept listId as an argument.
As a downside, the code looks more difficult to read to me. What would you actually like to achieve?
Closing due to inactivity