将构造参数传递给 redux 中间件
Passing construction parameters to redux middleware
我很好奇是否有一种方法可以在不从状态中检索参数的情况下将参数传递给中间件。我想要做的是传递一个我们正在使用的通用函数,该函数确定用户是否已通过身份验证。因此,我不想从代码重复的状态中检索身份验证信息,而是想将 isAuthenticated
函数传递给中间件。
我认为这不是在 applyMiddleware
框架中本地实现的,但也许有人可以解决这种情况。
由于传递给中间件的动作不必是纯粹的,您可以将函数作为动作的一部分传递。由于中间件可以访问存储,并使用 store.getState()
到状态,我们可以将该方法应用于状态,并获得计算结果。
在 real world example of redux 的 api 中间件中你可以看到 endpoint
可以是一个函数,实际端点可以从状态计算(见代码之间星号评论):
export default store => next => action => {
const callAPI = action[CALL_API]
if (typeof callAPI === 'undefined') {
return next(action)
}
let { endpoint } = callAPI
const { schema, types } = callAPI
/***************************************************************************/
/** if the endpoint is a function compute the actual endpoint from state ***/
if (typeof endpoint === 'function') {
endpoint = endpoint(store.getState())
}
/***************************************************************************/
if (typeof endpoint !== 'string') {
throw new Error('Specify a string endpoint URL.')
}
if (!schema) {
throw new Error('Specify one of the exported Schemas.')
}
if (!Array.isArray(types) || types.length !== 3) {
throw new Error('Expected an array of three action types.')
}
if (!types.every(type => typeof type === 'string')) {
throw new Error('Expected action types to be strings.')
}
function actionWith(data) {
const finalAction = Object.assign({}, action, data)
delete finalAction[CALL_API]
return finalAction
}
const [ requestType, successType, failureType ] = types
next(actionWith({ type: requestType }))
return callApi(endpoint, schema).then(
response => next(actionWith({
response,
type: successType
})),
error => next(actionWith({
type: failureType,
error: error.message || 'Something bad happened'
}))
)
}
好的,正确的方法是使用包装函数来包装实际的中间件功能
export const middlewareFunction = (store) => (next) => (action) => {
do some stuff with something...
}
如果这是您的实际中间件功能,那么您应该将中间件应用为
applyMiddleware(middlewareFunction);
传递参数你应该做的是实现一个像
这样的函数
export const middlewareWrapper = (args) => {
do some stuff with your args
return (state) => (next) => (action) => {
do more stuff with your args and actions
}
}
使用此语法,您可以将中间件应用为:
applyMiddleware(middlewareWrapper(args));
我认为正确的做法是再次咖喱。
你使用中间件的文件
import myMiddleWare from '/myMiddleWare.js'
import { applyMiddleware } from 'redux'
args = // whatever arguments you want
applyMiddleware(myMiddleWare(args))
myMiddleWare.js
export default (args) => ({getState, dispatch}) => (next) => (action) => (
// Use args do your hearts content
)
我很好奇是否有一种方法可以在不从状态中检索参数的情况下将参数传递给中间件。我想要做的是传递一个我们正在使用的通用函数,该函数确定用户是否已通过身份验证。因此,我不想从代码重复的状态中检索身份验证信息,而是想将 isAuthenticated
函数传递给中间件。
我认为这不是在 applyMiddleware
框架中本地实现的,但也许有人可以解决这种情况。
由于传递给中间件的动作不必是纯粹的,您可以将函数作为动作的一部分传递。由于中间件可以访问存储,并使用 store.getState()
到状态,我们可以将该方法应用于状态,并获得计算结果。
在 real world example of redux 的 api 中间件中你可以看到 endpoint
可以是一个函数,实际端点可以从状态计算(见代码之间星号评论):
export default store => next => action => {
const callAPI = action[CALL_API]
if (typeof callAPI === 'undefined') {
return next(action)
}
let { endpoint } = callAPI
const { schema, types } = callAPI
/***************************************************************************/
/** if the endpoint is a function compute the actual endpoint from state ***/
if (typeof endpoint === 'function') {
endpoint = endpoint(store.getState())
}
/***************************************************************************/
if (typeof endpoint !== 'string') {
throw new Error('Specify a string endpoint URL.')
}
if (!schema) {
throw new Error('Specify one of the exported Schemas.')
}
if (!Array.isArray(types) || types.length !== 3) {
throw new Error('Expected an array of three action types.')
}
if (!types.every(type => typeof type === 'string')) {
throw new Error('Expected action types to be strings.')
}
function actionWith(data) {
const finalAction = Object.assign({}, action, data)
delete finalAction[CALL_API]
return finalAction
}
const [ requestType, successType, failureType ] = types
next(actionWith({ type: requestType }))
return callApi(endpoint, schema).then(
response => next(actionWith({
response,
type: successType
})),
error => next(actionWith({
type: failureType,
error: error.message || 'Something bad happened'
}))
)
}
好的,正确的方法是使用包装函数来包装实际的中间件功能
export const middlewareFunction = (store) => (next) => (action) => {
do some stuff with something...
}
如果这是您的实际中间件功能,那么您应该将中间件应用为
applyMiddleware(middlewareFunction);
传递参数你应该做的是实现一个像
这样的函数export const middlewareWrapper = (args) => {
do some stuff with your args
return (state) => (next) => (action) => {
do more stuff with your args and actions
}
}
使用此语法,您可以将中间件应用为:
applyMiddleware(middlewareWrapper(args));
我认为正确的做法是再次咖喱。
你使用中间件的文件
import myMiddleWare from '/myMiddleWare.js'
import { applyMiddleware } from 'redux'
args = // whatever arguments you want
applyMiddleware(myMiddleWare(args))
myMiddleWare.js
export default (args) => ({getState, dispatch}) => (next) => (action) => (
// Use args do your hearts content
)