在我的 Redux 商店中,我有一组线程和一组回复。每个回复都有一个线程 ID 以将其与线程相关联。从数据库中获取线程时,回复计数是返回的属性之一,该计数显示在线程旁边的网页中。
当用户添加新回复时,我的挑战就会浮出水面。API 返回足够的信息让我将新回复添加到回复集合中。但我也想增加线程的回复计数属性,它位于线程数组内部。我该怎么做?
这些是我的(简化的)减速器:
const thread = (state = {}, action) => {
let nextState = state
if (action.type === C.POST_MESSAGE) {
nextState = action.payload
}
return nextState
}
const threads = (state = initialState.threads, action) => {
let nextState = state
if (action.type === C.POST_MESSAGE) {
nextState = [thread(null, action), ...state]
}
return nextState
}
const reply = (state = {}, action) => {
let nextState = state
if (action.type === C.POST_REPLY) {
nextState = action.payload
}
return nextState
}
const replies = (state = initialState.replies, action) => {
let nextState = state
if (action.type === C.POST_REPLY) {
nextState = [...state, action.payload]
}
return nextState
}