I'm following a tutorial in React Redux. I have created a store variable with Redux store which has two sub variables. One is expenses
which is an array of objects and another is filters
which is an object itself.
const store = createStore(
combineReducers({
expenses: expensesReducer,
filters: filtersReducer
})
);
When filled with dummy values the store would look like this:
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,
}
};
I'm currently writing a function to display resultant expenses array which looks like this.
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;
});
};
This function takes store.expenses
, store.filters
as two inputs. So I wanted to pass in store
object only and get the output.
Hence I tried to object destructure the input store itself instead of calling store below. But it returns an error.
const getVisibleExpenses = ({expenses, {text, sortBy, startDate, endDate}})
Is there any possible solutions?