简体   繁体   中英

Add an async code inside a redux action generator without returning a function and using redux-thunk?

Without using redux-thunk, I want to add an object(expense) to firebase realtime database inside the action generator function before returning the action itself. I don't see the need to redux-thunk if I am able to execute the async function. Here is the code in src/actions/expenses.js

export const  addExpense = ({description= '', note= '', createdAt= 0, amount= 0} = {}) => {
const expense = {
    id: uuidv4(),
    description,
    note,
    createdAt,
    amount
}

const db = firebase.database()
db.ref('expenses').push(expense)

return {
    type: 'ADD',
    expense
}

}

While it would technically work, this way you are making harder to write tests (see this example ).

If you don't care about tests... you probably didn't need redux in the first place

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM