reselect icon indicating copy to clipboard operation
reselect copied to clipboard

Question about memorized Multiple Components

Open exiadb opened this issue 9 years ago • 4 comments

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)
    }
  }
....

exiadb avatar May 19 '17 14:05 exiadb

I was wondering about something like this too to potentially decouple the selector from the component.

pixelshaded avatar May 22 '17 14:05 pixelshaded

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

marutha avatar May 29 '17 13:05 marutha

@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?

toomuchdesign avatar Jun 07 '17 13:06 toomuchdesign

Closing due to inactivity

ellbee avatar Jul 06 '17 18:07 ellbee