Skip to content

How to sort the objects with select #1757

Answered by markwhitfeld
bhagavan44 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @bhagavan44 The issue here is that your call the the sort method would cause a mutation of the state (javascript sort mutates the underlying array). I would recommend 2 things:

  1. Clone the array before sorting it:
const sorted = [...state.data].sort((a, b) => (a.name > b.name) ? -1 : 1);
  1. Use an input selector that is focused on the data property, as opposed to the whole account state.
    This would ensure that the selector would only recalculate when the data property changes, and not when any property of the AccountState changes.
    Note: for the following code, ensure that the selector option injectContainerState is set to false (see recommendation here) or move this selector out of the A…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@bhagavan44
Comment options

Answer selected by splincode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants