[英]Why would I use Redux Promise Middleware over Redux Promise?
I've used Redux Promise , but it seems Redux Promise Middleware has more functionality like dispatching multiple actions with "PENDING" or "FULFILLED" appended. 我使用过Redux Promise ,但似乎Redux Promise Middleware具有更多功能,例如通过附加“PENDING”或“FULFILLED”来调度多个动作。
Why would I use one over the other? 为什么我会使用一个而不是另一个?
I personally prefer Redux Promise Middleware as middleware as it enables optimistic updates; 我个人更喜欢Redux Promise Middleware作为中间件,因为它可以实现乐观的更新; dispatches pending, fulfilled and rejected actions;
发送未决,已履行和被拒绝的行动; and works well with with Redux Thunk to chain async actions.
并与Redux Thunk配合使用以链接异步操作。
For example, you can use actions with _PENDING
and _FULFILLED
in reducers and update the UI with progress bar and similar. 例如,您可以在reducers中使用带有
_PENDING
和_FULFILLED
操作,并使用进度条和类似更新UI。
There is an alternative to Redux Pomise Middleware. 还有Redux Pomise中间件的替代品。 Redux Auto has the same API as redux-promise-middleware and comes with a bunch of utility patterns under the hood to reduce the boilerplate you need to write.
Redux Auto与redux-promise-middleware具有相同的API,并带有一堆实用程序模式,以减少您需要编写的样板。
The idea is to have each action in a specific file . 我们的想法是将每个操作都放在特定的文件中 。 co-locating the server call in the file with reducer functions for "pending", "fulfilled" and "rejected".
将文件中的服务器调用与“pending”,“fulfilled”和“rejected”的reducer函数共同定位。 This makes handling promises very easy.
这使得处理承诺变得非常容易。
It also automatically attaches a helper object (called "async") to the prototype of your state, allowing you to track in your UI, requeste transitions. 它还会自动将辅助对象(称为“异步”)附加到您的状态原型,允许您在UI中跟踪请求转换。
Example: 例:
data/serverThing.js 数据/ serverThing.js
export function pending (posts, payload){
return posts
}
export function fulfilled (posts, payload, result){
return result.serverResponse
}
export function rejected (posts, payload, error){
return posts;
}
export function action (payload) {
return Promise.resolve({serverResponse: [1,2,3,4]})
}
UI UI
import React from "react"
import actions from 'redux-auto'
import { connect } from 'react-redux'
class Foobar extends Component {
const currentLoadingData = this.props.data.async.serverThing;
if(currentLoadingData instanceof Error){
var data = currentLoadingData.message
} else if(true === currentLoadingData ){
var data = "Loading..."
} else {
var data = this.porps.data.join();
}
render () {
return (
<div>
<button onClick={()=>actions.data.serverThing()}>Do something!</button>
{ data }
</div>
)
}
}
const mapStateToProps = ( { data }) => {
return { data }
};
export default connect( mapStateToProps )(Foobar);
To understand the above source.
要了解上述来源。 redux-auto automatically creates actions and wires them to reduces based on the file structure.
redux-auto会根据文件结构自动创建操作并将它们连接起来。 Where the folder name becomes the name of the property on the state.
文件夹名称成为状态属性的名称。 The files within a folder are actions to be performed on that part of the state.
文件夹中的文件是要对状态的该部分执行的操作。
Here is a complete redux-auto: helloworld project 这是一个完整的redux-auto:helloworld项目
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.