redux (React RTK)如何在createEntityAdapter中正确分配自定义selectId

v09wglhw  于 2022-11-12  发布在  React
关注(0)|答案(1)|浏览(96)

我的数据结构并不是严格意义上的数组,它更像是嵌套对象内部的数组。

{
    "start": "3-1-2021",
    "event_log": [
        {
            "event_id": 1,
            "rul": 61300,
            "start": "3-1-2021"
        },
        {
            "event_id": 2,
            "rul": 61299,
            "start": "3-1-2021"
        },
    ]
}

如何将event_id提取为实体并将其传递给createEntityAdapter?我已经尝试过了:

const chartAdapter = createEntityAdapter({
    selectId: (data) => data.event_log.map(item => item.event_id)
});

但是它不起作用。任何人都有一个想法,如何忽略start,并从event_log解构event_id,并将其传递到createEntityAdapter。非常感谢。

pprl5pva

pprl5pva1#

const EventLogAdapter = createEntityAdapter<EventLog>({
  selectId: log => log.event_id,
})

type State = Partial<{
  start: string
  // other fields
}> & {
  eventLogs: EntityState<EventLogAdapter>
}

const initialState: State = {
  eventLogs: EventLogAdapter.getInitialState(),
}

const LogsSlice = createSlice({
  name: 'logs',
  initialState,
  reducers: {
    // reducers
  },
})

答案与this相似

相关问题