Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad use of reselect #147

Open
guillaumearm opened this issue Jun 30, 2016 · 0 comments
Open

Bad use of reselect #147

guillaumearm opened this issue Jun 30, 2016 · 0 comments

Comments

@guillaumearm
Copy link
Contributor

guillaumearm commented Jun 30, 2016

Hi redpelicans team.
Recently, i worked with reselect, and I noticed that we use wrong

An example :

export const viewPersonSelector = createSelector(
  personId,
  companies,
  persons,
  missions,
  pendingRequests,
  (personId, companies, persons, missions, pendingRequests) => {
    return {
      person: persons.get(personId),
      company: personId ? companies.get(persons.get(personId).get('companyId')) : null,
      missions: missions.filter(getFilterMissionsById(personId)),
      companies: companies,
      persons: persons,
      isLoading: !!pendingRequests
    }
  }
)

Here, when a change occurs on pendingRequests, missions are filtered again.
The solution is to make lots of small selectors and compsoe them together.

// No need to memoize selectors that compute nothing
const getPersonId = state => state.routing.location && state.routing.location.state && state.routing.location.state.personId
const getIsLoading = state => !!state.pendingRequests
const getMissions = state => state.missions.data

// Here, missions are filtered only when 'missions.data' or 'personId' are updated.
const getFilteredMissions => createSelector(
  getMissions,
  getPersonId,
  (missions, personId) => missions.filter(getFilterMissionsById(personId))
)

const viewPersonSelector = createSelector(
  getIsLoading,
  getFilteredMissions,
  (isLoading, missions) => ({
    isLoading,
    missions,
    // etc...
  })
)
@guillaumearm guillaumearm changed the title Mauvaise utilisation de reselect Bad use of reselect Jul 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant