Redux Saga
1. 起步
import { Provider } from 'react-redux'
import reducer from './reducer'
import { createStore, applyMiddleware, compose } from 'redux'
import createSagaMiddleware from 'redux-saga'
import rootSaga from './sagas' // 引入 saga
import App from './app'
const sagaMiddleware = createSagaMiddleware() // 创建 saga 中间件
// 使用 chrome redux 调试工具
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
const store = createStore(reducer, composeEnhancers(
applyMiddleware(sagaMiddleware) // 使用中间件
))
sagaMiddleware.run(rootSaga) // 运行中间件
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)2. 实践
2.1 takeEvery 和 takeLatest
2.2 使用 call 而不是直接请求
2.3 使用 put 而不是直接 dispatch action
2.4 select 来获取当前 store 的 state
常见错误
参考资料
Last updated