一个对象中的项目如何从一个函数转移到另一个函数?

时间:2017-12-08 15:34:51

标签: react-redux redux-thunk

我正在学习react redux,我使用firebase来存储数据。

我安装了thunk中间件。一切正常,我只是不明白为什么。

据我了解,const费用是另一个功能范围内的对象。 addExpense如何访问它?

export const addExpense = (expense) => ({
    type: 'ADD_EXPENSE',
    expense
});

export const startAddExpense = (expenseData = {}) => {
    return (dispatch) => {
        const {
            description = '',
            note = '',
            amount = 0,
            createdAt = 0
        } = expenseData;
        const expense = { description, note, amount, createdAt };

        database.ref('expenses').push(expense).then((ref) => {
            dispatch(addExpense({
                id: ref.key,
                ...expense
            }));
        });
    };
};

1 个答案:

答案 0 :(得分:1)

startAddExpense正在将const expense对象与addExpense字段一起传递给您的id字段。恰好,addExpense的参数也称为expense,这可能会让您感到困惑。

希望能够解决它。