-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Closed
Description
When using the sorted state adapter, calling setMany with multiple entities sharing the same id results in duplicated IDs in state.ids.
The unsorted adapter works correctly.
This leads to inconsistent behavior between adapters and potentially unstable selector outputs.
import { createEntityAdapter } from '@reduxjs/toolkit'
interface BookModel {
id: string
title: string
author?: string
}
const adapter = createEntityAdapter<BookModel>({
selectId: (book) => book.id,
sortComparer: (a, b) => a.title.localeCompare(b.title),
})
const unsortedAdapter = createEntityAdapter<BookModel>({
selectId: (book) => book.id,
})
const state = adapter.getInitialState()
const AClockworkOrange = { id: 'aco', title: 'A Clockwork Orange' }
const withSorted = adapter.setMany(state, [
{ ...AClockworkOrange, author: 'A' },
{ ...AClockworkOrange, title: 'Zack' },
])
const withUnsorted = unsortedAdapter.setMany(state, [
{ ...AClockworkOrange, author: 'A' },
{ ...AClockworkOrange, title: 'Zack' },
])
console.log(withSorted) // => {ids: ['aco', 'aco'], entities: {aco: {id: 'aco', title: 'Zack'}}}
console.log(withUnsorted) // => {ids: ['aco'], entities: {aco: {id: 'aco', title: 'Zack'}}}
Expected behavior:
- Both adapters should behave consistently.
- For setMany, the last occurrence of an entity ID should be preserved.
- The state.ids array should not contain duplicates.
vilgeforc5 and igor-tmrw
Metadata
Metadata
Assignees
Labels
No labels