使用redux和react进行嵌套对象分解
我正在学习React Redux的教程。我用Redux store创建了一个store变量,它有两个子变量。一个是使用redux和react进行嵌套对象分解,redux,object-destructuring,Redux,Object Destructuring,我正在学习React Redux的教程。我用Redux store创建了一个store变量,它有两个子变量。一个是费用,它是一个对象数组,另一个是过滤器,它是一个对象本身 const store = createStore( combineReducers({ expenses: expensesReducer, filters: filtersReducer }) ); 当填充虚拟值时,存储将如下所示: const dummyState = {
费用
,它是一个对象数组,另一个是过滤器
,它是一个对象本身
const store = createStore(
combineReducers({
expenses: expensesReducer,
filters: filtersReducer
})
);
当填充虚拟值时,存储将如下所示:
const dummyState = {
expenses: [{
id: '10-AC-191',
title: 'January Rent',
note: 'This was the final payment for that address',
amount: 545.00,
createdAt: 0
}, {
id: '10-AK-155',
title: 'Breakfast',
amount: 545.00,
createdAt: 2000
}],
filters: {
text: 'rent',
sortBy: 'amount',
startDate: 700,
endDate: 360,
}
};
我目前正在编写一个函数来显示结果费用数组,如下所示
const getVisibleExpenses = (expenses, {text, sortBy, startDate, endDate}) => {
return expenses.filter(({title, note, createdAt}) => {
const startDateMatch = typeof startDate !== 'number' || createdAt >= startDate;
const endDateMatch = typeof endDate !== 'number' || createdAt <= endDate;
const searchText = text.trim().toLowerCase();
const textMatch = typeof text !== 'string' || title.toLowerCase().includes(searchText)
|| note.toLowerCase().includes(searchText);
return startDateMatch && endDateMatch && textMatch;
}).sort((expense_a, expense_b) => {
if (sortBy === 'amount') return expense_a.amount - expense_b.amount;
else if (sortBy === 'date') return expense_a.createdAt - expense_b.createdAt;
});
};
有什么可能的解决方案吗?试试这个:
const getVisibleExpenses = ({expenses, filters: {text, sortBy, startDate, endDate}}) => {}
const getVisibleExpenses = ({expenses, filters: {text, sortBy, startDate, endDate}}) => {}