尝试了解在商店中使用自定义中间件的React-Redux代码库。自定义中间件使用API客户端功能,该功能管理对传递给它的操作的API调用。
在中间件中,有一个我无法理解的变量声明。它使用数组作为变量。
const [REQUEST, SUCCESS, FAILURE] = types;
// types: [REGISTER, REGISTER_SUCCESS, REGISTER_FAIL],
reducer看起来像这样:
case LOAD:
return {
...state,
loading: true
};
case LOAD_SUCCESS:
saveAuthCookie( action.result );
return {
...state,
loading: false,
loaded: true,
user: cookie.load( userCookieName ) ? cookie.load( userCookieName ) : action.result
};
case LOAD_FAIL:
console.log(`auth LOAD_FAIL with error ${util.inspect(action.error)}`);
return {
...state,
loading: false,
loaded: false,
error: action.error
};
正在调用的reducer导出的函数如下所示:
export function load() {
return {
types: [LOAD, LOAD_SUCCESS, LOAD_FAIL],
promise: (client) => client.get( '/auth/loadAuth' )
};
}