没有调用React saga函数

时间:2018-05-24 00:15:57

标签: javascript reactjs react-native redux redux-saga

我一直在开发一个本机反应项目,我试图调用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 });
}

以上是我的代码。我的应用程序运行时没有错误。但传奇并没有被称为传奇。当点击touchableopacity时。

1 个答案:

答案 0 :(得分:0)

rootSaga.js:

import { all } from 'redux-saga/effects';
import { loginFlow } from './AuthSagas';

export default function* rootSaga() {
  yield all([
    loginFlow(),
  ]);
}