I started using reselect in my React Native project. I was looking for a way to work with derived data in a redux application that works with large sets of Contacts.
I have a problem with sorting. My main contact list sorts by a numeric field named "delta":
const contactsSelector = state => state.contacts
const sortedContactsSelector = createSelector(
contactsSelector,
contacts => contacts.sort(function (a, b) {return a.delta - b.delta})
)
const mapStateToProps = (state, ownProps) => {
return {
contacts: sortedContactsSelector(state)
}
}
It works.
However when I present a modal with a list that is sorted by the contact's name:
const contactsSelector = state => state.contacts
const sortedContactsSelector = createSelector(
contactsSelector,
contacts => contacts.sort(function (a, b) {
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
})
)
const mapStateToProps = (state, ownProps) => {
return {
contacts: sortedContactsSelector(state)
}
}
And closes the modal - the original main contact screen is sorted by name (as the modal).
Any way around this?
Is the best practice here is cloning:
const contactsSelector = state => state.contacts
const sortedContactsSelector = createSelector(
contactsSelector,
contacts => clone(contacts).sort(function (a, b) {
if (a.name < b.name)
return -1;
if (a.name > b.name)
return 1;
return 0;
})
)