Redux saga worker没有被调用

时间:2018-06-08 14:56:33

标签: redux react-redux redux-saga

我不知道为什么,其中一个传奇工作者没有被召唤。

sagas / login.js login工作人员,我在 actions / profile.js 中调用另一个操作getProfile

调用

yield put({ type: ActionTypes.LOGIN_SUCCEEDED, address: account.address });并调用getProfile操作,但 sagas / profile.js 中的getProfile未被调用。

Home.jsx

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import login from 'actions/login';

class Home extends Component {
  static propTypes = {
    login: PropTypes.func,
  };

  static defaultProps = {
    login: () => null,
  };

  submit = (e) => {
    e.preventDefault();
    this.props.login(this.key.value);
  };

  render() {
    return (
      <div>
        <form onSubmit={this.submit}>
          <div className="input-group">
            <input
              type="password"
              className="form-control"
              ref={el => (this.key = el)}
            />
            <button type="submit" className="btn btn-primary">
              Login
            </button>
          </div>
        </form>
      </div>
    );
  }
}

const mapDispatchToProps = dispatch => ({
  login: key => dispatch(login(key)),
});

export default connect(
  null,
  mapDispatchToProps,
)(Home);

操作/ login.js

import * as ActionTypes from '../constants/actionTypes';

const login = key => ({
  type: ActionTypes.LOGIN_REQUESTED,
  key,
});

export default login;

操作/ profile.js

import * as ActionTypes from '../constants/actionTypes';

const getProfile = id => ({
  type: ActionTypes.PROFILE_REQUESTED,
  id,
});

export default getProfile;

传奇/ index.js

import { all, fork } from 'redux-saga/effects';
import watchLogin from './login';
import watchProfile from './balance';

export default function* rootSaga() {
  yield all([
    fork(watchLogin),
    fork(watchProfile),
  ]);
}

传奇/ login.js

import { fork, put, takeLatest } from 'redux-saga/effects';
import { wallet } from '@cityofzion/neon-js';
import getProfile from 'actions/profile';
import * as ActionTypes from '../constants/actionTypes';

function* login(action) {
  const { key } = action;
  try {
    const account = new wallet.Account(key);
    yield call(getProfile, account.id);
    yield put({ type: ActionTypes.LOGIN_SUCCEEDED, address: account.address });
  } catch (error) {
    yield put({ type: ActionTypes.LOGIN_FAILED, message: error.message });
  }
}

export default function* watchLogin() {
  yield takeLatest(ActionTypes.LOGIN_REQUESTED, login);
}

传奇/ profile.js

import { call, put, takeLatest } from 'redux-saga/effects';
import { api, wallet } from '@cityofzion/neon-js';
import * as ActionTypes from '../constants/actionTypes';

function* getProfile(action) {
  const { id } = action;
  try {
    const profile = yield call(
    get,
    id,
  );
    yield put({ type: ActionTypes.PROFILE_SUCCEEDED, profile });
  } catch (error) {
    yield put({ type: ActionTypes.PROFILE_FAILED, message: error.message });
  }
}

export default function* watchProfile() {
  yield takeLatest(ActionTypes.PROFILE_REQUESTED, getProfile);
}

index.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import createSagaMiddleware from 'redux-saga';
import { composeWithDevTools } from 'redux-devtools-extension/developmentOnly';

import App from 'components/App';
import reducers from 'state/index';
import sagas from 'sagas/index';

const sagaMiddleware = createSagaMiddleware();

const store = createStore(
  combineReducers({
    ...reducers,
  }),
  composeWithDevTools(applyMiddleware(
    sagaMiddleware,
  )),
);

sagaMiddleware.run(sagas);

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('app'),
);

的package.json

"dependencies": {
    "@cityofzion/neon-js": "^3.8.1",
    "axios": "^0.18.0",
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "react-redux": "^5.0.7",
    "react-slidedown": "^1.3.0",
    "redux": "^4.0.0",
    "redux-saga": "^0.16.0"
  },

2 个答案:

答案 0 :(得分:0)

您需要*watch

中的while(true)传奇
export default function* watchProfile() {
  while(true) {
    yield takeLatest(ActionTypes.PROFILE_REQUESTED, getProfile);
  }
}

答案 1 :(得分:0)

而不是yield call我需要使用yield put

我将yield call(getProfile, account.id);更改为yield put(getProfile, account.id);,现在可以正常工作。