将 `compose` 用于中间件或将它们列在 `applyMiddleware` 中有什么区别
What difference between using `compose` for middleware or list them in `applyMiddleware`
这些 configureStore
函数之间的区别是什么?initialState
参数在哪里?
import { createStore, applyMiddleware } from 'redux';
import logger from 'redux-logger';
import thunk from 'redux-thunk';
import rootReducer from '../reducers';
export default function configureStore(initialState){
const store = createStore(
rootReducer,
initialState,
applyMiddleware(thunk, logger) //list of middlewares in arguments
);
return store;
}
export default function configureStore() {
const store = compose( //composed middlewares
applyMiddleware(thunk),
applyMiddleware(logger)
)(createStore)(rootReducer);
return store;
}
All compose does is let you write deeply nested function transformations without the rightward drift of the code. Don't give it too much credit!
及其用法:
This is a functional programming utility, and is included in Redux as a convenience.
You might want to use it to apply several store enhancers in a row.
所以本质上,如果你想应用一堆中间件,它是一个有用的实用程序:
...
compose(
applyMiddleware(thunk),
DevTools.instrument(),
// ... more enhancements, that may or may not be middleware
)
...
applyMiddleware
专门用于包装商店的 dispatch
功能,而 compose 将允许您添加可能与调度无关的其他增强器,例如 Redux Dev Tools。
Middleware only wraps the store's dispatch function. Technically, anything a middleware can do, you can do manually by wrapping every dispatch call, but it's easier to manage this in a single place and define action transformations on the scale of the whole project.
这些 configureStore
函数之间的区别是什么?initialState
参数在哪里?
import { createStore, applyMiddleware } from 'redux';
import logger from 'redux-logger';
import thunk from 'redux-thunk';
import rootReducer from '../reducers';
export default function configureStore(initialState){
const store = createStore(
rootReducer,
initialState,
applyMiddleware(thunk, logger) //list of middlewares in arguments
);
return store;
}
export default function configureStore() {
const store = compose( //composed middlewares
applyMiddleware(thunk),
applyMiddleware(logger)
)(createStore)(rootReducer);
return store;
}
All compose does is let you write deeply nested function transformations without the rightward drift of the code. Don't give it too much credit!
及其用法:
This is a functional programming utility, and is included in Redux as a convenience. You might want to use it to apply several store enhancers in a row.
所以本质上,如果你想应用一堆中间件,它是一个有用的实用程序:
...
compose(
applyMiddleware(thunk),
DevTools.instrument(),
// ... more enhancements, that may or may not be middleware
)
...
applyMiddleware
专门用于包装商店的 dispatch
功能,而 compose 将允许您添加可能与调度无关的其他增强器,例如 Redux Dev Tools。
Middleware only wraps the store's dispatch function. Technically, anything a middleware can do, you can do manually by wrapping every dispatch call, but it's easier to manage this in a single place and define action transformations on the scale of the whole project.