简体   繁体   English

在 redux 动作生成器中添加异步代码而不返回 function 并使用 redux-thunk?

[英]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.在不使用 redux-thunk 的情况下,我想在操作生成器 function 内的 firebase 实时数据库中添加一个对象(费用),然后再返回操作本身。 I don't see the need to redux-thunk if I am able to execute the async function.如果我能够执行异步 function,我认为不需要 redux-thunk。 Here is the code in src/actions/expenses.js这是 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如果您不关心测试...您可能首先不需要 redux

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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