javascript - 未调用 React saga 函数
问题描述
我一直在开发一个 react-native 项目,我正在尝试调用 redux-saga 函数。它没有被调用(我没有收到任何错误)。
//App.JS
....
import configureStore from './src/store/configureStore';
const store = configureStore();
class App extends Component {
render() {
return (
<Provider store={store}>
<View style={{ flex: 1 }}>
<StatusBar backgroundColor="#285576" />
<MyForm />
</View>
</Provider>
);
}
}
export default App;
这是我的传奇传奇配置
import { applyMiddleware, createStore } from 'redux';
import createSagaMiddleware from 'redux-saga';
import reducers from './../reducers';
import rootSaga from './../sagas/rootSaga';
export default function configureStore() {
const sagaMiddleware = createSagaMiddleware();
const store = createStore(reducers, applyMiddleware(sagaMiddleware));
sagaMiddleware.run(rootSaga);
return store;
}
这是我调用登录操作的组件
...
<TouchableOpacity
style={{
backgroundColor: '#4368b2',
borderColor: '#3464c8',
borderRadius: 5
}}
onPress={login(this.props.email,this.props.password)}> //call login action
<Text style={commonStyle.buttonText}>{this.props.type}</Text>
</TouchableOpacity>
...
这是动作文件。当我点击 TouchableOpacity 时,console.log 正在触发
export function login(email, password) {
console.log('action-login');
return {
type: LOGIN_ACTION,
username: email,
password
};
}
这是我的 rootSaga.js
import { all, fork } from 'redux-saga/effects';
import { loginFlow } from './AuthSagas';
export default function* rootSaga() {
yield fork(loginFlow);
}
这是 AuthSagas.js 。当应用程序启动时 loginFlow 功能的 console.log 正在工作
import { put, call, take } from 'redux-saga/effects';
import { takeEvery } from 'redux-saga';
import auth from './../auth';
import { LOGIN_ACTION } from './../action/types';
import { setLoginSuccess, setLoginError } from './../action';
function* authorize(credentials) {
try {
const token = yield call(auth.login(credentials));
console.log(token);
if (!token.error) {
yield put(setLoginSuccess(token, credentials.username, credentials.password));
return token;
}
yield put(setLoginError(token));
return undefined;
} catch (error) {
console.log(error);
return undefined;
}
}
export function* loginFlow() {
console.log('saga-alert');
const { username, password } = yield take(LOGIN_ACTION);
yield call(authorize, { username, password });
}
以上是我的代码。我的应用程序运行没有错误。但 saga 并没有被调用。当点击 touchableopacity 时。
解决方案
rootSaga.js:
import { all } from 'redux-saga/effects';
import { loginFlow } from './AuthSagas';
export default function* rootSaga() {
yield all([
loginFlow(),
]);
}
推荐阅读
- python - 如何比较数据并从 pandas 的 multiIndex 数据框中选择 TOP 2?
- python - Spyder环境和Anaconda提示指向不同的解释器
- javascript - 从 JavaScript REGEX 获取分组数组
- javascript - 在基于反应的应用程序中包含 js 文件?
- python - numpy 掩码不适用于两侧的有界范围
- jquery - 使用 jQuery 使用前一个元素复制和替换文本
- macos - 在设置了preferredContentSize的特定视图控制器上静音“没有任何布局约束的视图可能会剪辑其内容”
- c# - C# 读取 XPS 导致内存泄漏
- python - 看不懂这行 Python 代码
- flutter - 当项目具有动态高度时,如何根据设备方向创建动态颤动网格布局