我有这个saga-reducer-store代码,理论上应该在dispatch
上触发一个传奇事件,但我发现根本没有动作。这段代码可以用于反应。
相关的SO问题是Redux saga - Worker saga not getting called on react native,但由于take*
表单redux-saga
已被弃用,解决方案已过时。
以下代码 -
rootSaga.js
import { takeEvery } from "redux-saga/effects";
export function* rootSaga() {
yield takeEvery(LoginSagaConstants.LOGIN, userLogin)
}
loginSaga.js
export function* userLogin(action) {
console.log("userLogin not getting called");
const response = yield call(LoginAPI.login, action.user);
console.log(response);
...
}
loginScreen.js
console.log("start dispatching");
dispatch({
type: sagaConstants.LOGIN,
user: {
username: "username",
password: "password",
robottest : "robottest"
}
});
store.js
import {createStore, applyMiddleware, compose} from "redux";
import {createLogger} from 'redux-logger'
import createSagaMiddleware from 'redux-saga'
const initialState = {};
// import all Reducers
import {reducers} from "./reducers";
// import all Sagas
import { rootSaga } from "./sagas";
// define middlewares
let middlewares = [];
// create and add the saga middleware
const sagaMiddleware = createSagaMiddleware();
middlewares.push(sagaMiddleware);
//add the freeze and logger dev middleware
if (process.env.NODE_ENV !== 'production') {
middlewares.push(createLogger());
}
// apply the middleware
let middleware = applyMiddleware(...middlewares);
const enhancer = compose(
middleware
)
export default function configureStore() {
const store = createStore(reducers, initialState, enhancer);
// run saga middleware
sagaMiddleware.run(rootSaga);
return store;
}
App.js
...
import configureStore from './store'
const store = configureStore();
...
render() {
return (
<Provider store={store}>
<RootStack/>
</Provider>
);
}
我的控制台只打印start dispatching
,没有其他事情发生。