Prevent react and reselect from sorting items

570 Views Asked by At

I am using React and Redux. I have a store of items like {name: 'Jim', votes: '5', type: 'candidate'}

I am using reselect to get a filtered list of items. I am also using reselect to sort this list of items by votes.

When an item is voted it will automatically change position in the list (item changes, reselect does its job and reorder the list).

I would like to prevent this behavior. Is that possible without re-ordering inside the state?

2

There are 2 best solutions below

2
On BEST ANSWER

Another approach might consist of generating the sorting information during data normalization or items initialization. You might keep the information into a separate array or just push a sorting propery into each item object.

In that way updating the score wouldn't interfere with item sorting.

5
On

I guess you could introduce a new property in the Redux state like shouldSort which you initialize to true. Then, in your updateVote(newValue) action (or what ever you call it), you let the reducer for that action also set shouldSort to false.

Now you could use shouldSort in your reselect function to determine if it should or shouldn't sort.

Note that you would also need to reset the state of shouldSort when you enter/leave the page. Or else you may not get a sorted list when you leave and enter the page again.