gpt4 book ai didi

javascript - React saga 函数未被调用

转载 作者:行者123 更新时间:2023-11-29 23:21:20 25 4
gpt4 key购买 nike

我一直在开发一个 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;

这是我的saga saga配置

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>
...

这是 Action 文件。当我被点击时 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(),
]);
}

关于javascript - React saga 函数未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50499170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com