为什么要使用 Redux Thunk [英] Why use Redux Thunk
问题描述
为什么使用 Redux Thunk 那么你可以这样做:
Why use Redux Thunk then one can do something like this:
ReadableAPI.getCategories().then((categories)=>{
console.log('after getCategories', categories)
this.props.dispatch(addCategories(categories))
})
这不是更直接并达到同样的目的吗?
Isn't this more straightforward and achieves the same thing?
推荐答案
这个来自 Dan Abramov 的回答很好地解释了为什么你想要在您的应用程序中使用 redux-thunk.在您的应用程序中使用 redux-thunk 的另一个好处是,您可以将业务逻辑与视图部分分开(在您的情况下是 React).我们有一个用例,我们的应用程序是用 backbone
编写的,我们想用 React 重写整个应用程序.我们意识到如果您的视图和集合/模型是分开的,这很容易.我们开始只贬低 html 模板而不是集合.当我们弃用所有 html 模板时,我们开始在我们的应用中使用 Redux
并弃用我们的集合.
This answer from Dan Abramov explains beautifully why you would want to use redux-thunk in your application. One more benefit of using redux-thunk in your application is that you keep your business logic separate from your view part (React in your case). We had a use case where our app was written in backbone
and we wanted to re-write our whole app in React. We realised that it is easy if your view and collection/models are separate. We started depreciating just the html templates and not the collections. When we deprecated all the html templates, we started using Redux
in our app and deprecated our collections too.
我想说的是,我们将 view
和 business
逻辑分开,我们可以轻松地重构.类似地,对于 React
和 Redux
,你会希望保持不同,这样如果有新的东西出现并取代了 Redux
,至少你赢了不必反对您的观点,您只需要更改您的业务逻辑.
What I want to put here is that we had our view
and business
logic separate, we could refactor that easily. Similarly, with React
and Redux
, you would want to keep that different, so that if something new comes and replaces Redux
, at least you won't have to deprecate your views and you just will have to change your business logic.
这篇关于为什么要使用 Redux Thunk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!