世界上最伟大的投资就是投资自己的教育
https://redux.js.org/api-reference/createstore
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import { createStore, applyMiddleware } from 'redux';
import rootReducer from './reducers';
import { Provider } from 'react-redux';
const logger = store => next => action => {
console.log('dispatching', action);
let result = next(action);
console.log('next state', store.getState());
return result;
};
const error = store => next => action => {
try {
next(action)
} catch(e) {
console.log('error ' + e);
}
};
// const logger = function(store) {
// return function(next) {
// return function(action) {
// console.log('dispatching', action);
// let result = next(action);
// console.log('next state', store.getState());
// return result;
// }
// }
// }
const store = createStore(rootReducer, {}, applyMiddleware(logger, error));
// store.subscribe(() => console.log("State updated!", store.getState()));
ReactDOM.render(
<Provider store={ store }>
<App />
</Provider>,
document.getElementById('root')
);
registerServiceWorker();
reducers/counter.js
const counter = (state = 1, action = {}) => {
switch(action.type) {
case 'INCREMENT':
throw new Error('error in INCREMENT')
// return state + 1;
case 'DECREMENT':
return state - 1;
default: return state;
}
}
export default counter;
05:291FreeRedux 入门教程 #1 课程介绍
09:492FreeRedux 入门教程 #2 为什么需要 Redux
08:593FreeRedux 入门教程 #3 什么是 Redux
04:194ProRedux 入门教程 #4 创建页面
11:225ProRedux 入门教程 #5 单独使用 Redux
10:506ProRedux 入门教程 #6 使用 React-redux
05:337ProRedux 入门教程 #7 mapStateToProps 和 combineReducers
05:408ProRedux 入门教程 #8 dispatch 和 action
03:429ProRedux 入门教程 #9 mapDispatchToProps
03:4710ProRedux 入门教程 #10 bindActionCreators
09:1611ProRedux 入门教程 #11 装饰器函数 @connect
08:54FreeRedux 入门教程 #12 中间件
02:0913ProRedux 入门教程 #13 redux-logger
08:0414ProRedux 入门教程 #14 异步和 redux-thunk
08:3815FreeRedux 入门教程 #15 redux-thunk 实践发送 ajax 请求 part 1
07:2216ProRedux 入门教程 #16 redux-thunk 实践发送 ajax 请求 part 2
09:5117ProRedux 入门教程 #17 异步与 promise
03:1118ProRedux 入门教程 #18 调试工具 Redux DevTools
07:3119ProRedux 入门教程 #19 configureStore
03:5220ProRedux 入门教程 #20 配置热模块加载 hmr(完结)
▬▬▬▬▬▬ 联系我 👋 ▬▬▬▬▬▬
微信:qiuzhi99pro
b 站:https://space.bilibili.com/31152817
知乎:https://www.zhihu.com/people/rails365
Github:https://github.com/hfpp2012
Youtube:https://www.youtube.com/channel/UCA-Jkgr40A9kl5vsIqg-BIg
© 汕尾市求知科技有限公司 | 创业者社区 | Rails365 Gitlab | Qiuzhi99 Gitlab | Railstart 创业项目 | 知乎 | b 站 | 搜索
粤公网安备 44152102000088号
| 粤ICP备19038915号
这里其实是有很多疑问没有解开的。
applyMiddleware() 背后是有函数组合的思想的。
中间件的写法为什么会是 store => next => action 的这种写法,这背后是函数柯里化的思想
说到函数柯里化和函数组合,就不的不说到函数式编程的思想了
👍
let next=store.dispatch;
store.dispatch=(action)=>{}
系统日志提示
next () 应该怎么样理解 那块 逻辑有点不好理解
return result 是多余的,感觉是洋葱圈模型
感觉应该不是 const error 和 logger 都是有赋值 并且在 applyMiddleWare 里面做了传参