我试图在我的notes应用程序中实现排序功能,但遇到了一些问题。我的排序功能必须只反转一个notes数组,但它不起作用。
I have a state with notes:
export const initialState = {
notes: [
{
id: nanoid(),
text: '',
date: '',
},
],
}
Action:
export const sortNoteAction = ([notes]) => ({
type: SORT_NOTE,
payload: [notes],
})
Reducer:
export default function notes(state = initialState, { type, payload }) {
switch (type) {
case SORT_NOTE: {
return {
...state,
notes: [payload].reverse(),
}
}
default:
return state
}
}
Action:
export const sortNoteAction = (notes=[]) => ({
type: SORT_NOTE,
payload: notes,
})
Reducer:
export default function notes(state = initialState, action) {
switch (action.type) {
case SORT_NOTE: {
return {
...state,
notes: action.payload.reverse(),
}
}
default:
return state
}
}
我认为它不起作用,因为您实际上是在用一个项目对列表进行排序。如果将[notes]
更改为notes
(出现2次(,将[payload]
更改为payload
(出现1次(,则可能会工作