我必须按升序或降序对一组对象进行排序,但遇到了麻烦。我的 redux 状态正在 redux 开发工具中更新,并且一切正常,但它没有在组件上显示我的更改,也没有重新渲染。
功能:
const sortAsc = () => {
const sortAscending = productsFiltered.sort((a, b) => a.price - b.price);
console.log(sortAscending);
dispatch(sortProducts(sortAscending));
};
const sortDesc = () => {
const sortDescending = productsFiltered.sort((a, b) => a.price - b.price).reverse();
console.log(sortDescending);
dispatch(sortProducts(sortDescending));
};
<button onClick={sortAsc}>sortAsc</button>
<button onClick={sortDesc}>sortDesc</button>
操作:
export const sortProducts = (products) => {
return {
type: ActionTypes.SORT_PRODUCTS,
payload: products,
};
};
减速器:
export const FilterCategoryReducer = (state = [], action) => {
switch (action.type) {
case ActionTypes.FILTER_CATEGORY:
return { ...state, products: action.payload };
case ActionTypes.SORT_PRODUCTS:
return { products: action.payload };
default:
return state;
}
};