Javascript Redux中间件设计re:返回值
所以我刚刚读过redux中间件,听起来很棒。但有一件事困扰着我——中间件的返回值 我知道一些中间件实例返回东西(即Javascript Redux中间件设计re:返回值,javascript,redux,middleware,redux-promise,Javascript,Redux,Middleware,Redux Promise,所以我刚刚读过redux中间件,听起来很棒。但有一件事困扰着我——中间件的返回值 我知道一些中间件实例返回东西(即redux-promise),而我得到另一个中间件(即logging)不返回东西,只返回next(action)的结果 我的问题是,如果我想使用两个都返回内容的中间件,会发生什么情况?它们肯定会相互碰撞,我只会得到最外层中间件的返回值 express/connect中间件通过让中间件将其“结果”写入req和res对象来解决这个问题,但是redux的解决方案是什么 编辑 以下是我的问题
redux-promise
),而我得到另一个中间件(即logging
)不返回东西,只返回next(action)
的结果
我的问题是,如果我想使用两个都返回内容的中间件,会发生什么情况?它们肯定会相互碰撞,我只会得到最外层中间件的返回值
express/connect
中间件通过让中间件将其“结果”写入req
和res
对象来解决这个问题,但是redux的解决方案是什么
编辑
以下是我的问题的一个更具体的例子:
我有两个中间件:
根据我链接这两位中间件的顺序,我的
分派(操作)
的结果将是延迟取消fn或数字5。但是如何获得这两个结果呢?请查看applyMiddleware
上的文档。它解释了将中间件编写为可组合的,这样就可以将其插入到中间件链中,而不必担心在其前后应用的中间件:
中间件的关键特性是它是可组合的。倍数
中间件可以组合在一起,每个中间件不需要
链中在其之前或之后的知识
文档很好地解释了要传递到中间件的参数和预期的返回
下面是一个可运行的脚本,它演示了我试图(但未能)描述的问题。它还包括一个潜在的解决方案(使用中间件包装器)。我很想知道是否还有更优雅的解决方案
var { createStore, applyMiddleware } = require( "redux" );
var dispatchResult;
// create the results object to be passed along the middleware chain, collecting
// results as it goes
const genesis = _store => next => action => {
next( action );
return {};
};
const wrapper = ( key, mware ) => store => next => action => {
// extract the results object by storing the result of next(action)
// when it is called within the middleware
var extractedResult;
function modifiedNext( action ) {
extractedResult = next( action );
return extractedResult;
}
// get the result of this middleware and append it to the results object
// then pass on said results object...
var newResult = mware( store )( modifiedNext )( action );
extractedResult[ key ] = newResult;
return extractedResult;
};
// create standard logging middleware
const logger = store => next => action => {
let result = next( action );
console.log( `value is: ${ store.getState() }.`);
return result;
};
// create middleware that returns a number
const gimme = val => _store => next => action => {
next( action );
return val;
};
// create our super simple counter incrementer reduer
function reducer( state = 0, action ) {
if( action.type === "INC" )
return state + 1;
return state;
}
// first lets try running this without the wrapper:
dispatchResult = createStore( reducer, applyMiddleware(
gimme( 4 ),
logger,
gimme( 5 )
) ).dispatch( { type : "INC" } );
// will return only 4 (the result of the outermost middleware)
// we have lost the 5 from the gimme(5) middleware
console.log( dispatchResult );
// now we include the middleware wrapper and genesis middleware
dispatchResult = createStore( reducer, applyMiddleware(
wrapper( "g4", gimme( 4 ) ),
logger,
wrapper( "g5", gimme( 5 ) ),
genesis
) ).dispatch( { type : "INC" } );
// we will now return { g4 : 4, g5 : 5 }
// we have preserved the results of both middlewares
console.log( dispatchResult );
您缺少中间件的要点,它是一个用于消费和调度操作的管道。返回值通常被忽略。Hi-Yo,感谢您的回复!浏览了链接页面,但仍然觉得我的问题没有得到回答。我已经编辑了我的原始问题,以提供一个具体的ish示例来提高清晰度。谢谢我不确定我是否理解你的例子。中间件需要接受两个参数,
getState
和dispatch
,并返回一个返回next(action)
的函数。中间件签名对于每个中间件都是相同的,因此不会有返回5
的中间件。在文档中:“中间件签名是({getState,dispatch})=>next=>action
”@YoWakita我认为maambmb指的是与Redux注释中引用的相同的“任意返回”:这就是Redux中间件的设计行为。大多数中间件只需传回从next()
接收到的任何返回值,但如果中间件愿意,则完全可以返回其他内容。这在很大程度上归结为JS函数只返回一个值。一个好答案的路径可能从Redux Typescript定义开始:作为参考,这是Redux注释中提到任意返回值行为的注释,顺便说一句: