如何在组件上按顺序调用传奇?

时间:2018-05-14 11:53:29

标签: reactjs redux redux-saga

问题

我想在按func()按钮时拨打increase_number and decrease_number,但我不知道该怎么做。

我能这样做吗? (它会增加一个数字然后减少。)

或者我是否必须创建一个新的动作和传奇?

感谢阅读!

源代码

Redux(与传奇)

import { createAction, handleActions } from 'redux-actions';
import { delay } from 'redux-saga';
import { all, put, select, takeEvery } from 'redux-saga/effects';

export const action_creators = {
    increase_number: createAction('increase_number'),
    decrease_number: createAction('decrease_number'),
    set_number_success: createAction('set_number_success')
};

const state = {
    number: 0
}

function* increase_number () {
    const state = yield select((state) => state);
    yield delay(1000);
    yield put(action_creators.set_number_success({ number:++state.number }));
}

function* decrease_number() {
    const state = yield select((state) => state);
    yield put(action_creators.set_number_success({ number:--state.number }));
}

export function* saga() {
    yield all([
        takeEvery('increase_number', increase_number),
        takeEvery('decrease_number', decrease_number)
    ]);
}

export default handleActions({
    set_number_success: (state, action) => {
        const new_number = action.payload.number;
        console.log(new_number);
        return { ...state, number:new_number };
    }
}, state);

组件

import * as React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { put, take } from 'redux-saga/effects';
import { action_creators } from './redux';

function* func () {
    yield put(action_creators.increase_number());
    yield take(action_creators.set_number_success());
    yield put(action_creators.decrease_number());
}

class App extends React.PureComponent {
    render() {
        return [
            <div>{this.props.number}</div>,
            <button type="button" onClick={() => this.props.increase_number()}>increase_number</button>,
            <button type="button" onClick={() => this.props.decrease_number()}>decrease_number</button>,
            <button type="button">increase_number and decrease_number</button> <= Here.
        ]
    }
}

function map_state_to_props(state) {
    return {
        number: state.number
    }
}

function map_dispatch_to_props(dispatch) {
    return bindActionCreators({
        increase_number: action_creators.increase_number,
        decrease_number: action_creators.decrease_number
    }, dispatch);
}

export default connect(map_state_to_props, map_dispatch_to_props)(App);

URL for test

1 个答案:

答案 0 :(得分:1)

首先,你的func应该是saga而不是组件,因为它是一个传奇。

其次,是的,你需要一个单独的动作,因为它是一个单独的副作用,它结合了另外两个副作用,但仍然。

通过添加另一个takeEvery()

,您可以实现最简单的目标
...

function* increase_and_decrease() {
  yield call(increase);
  yield call(decrease);
}


export function* saga() {
  yield all([
    takeEvery('increase_number', increase_number),
    takeEvery('decrease_number', decrease_number),
    takeEvery('incerase_and_decrease', increase_and_decrease);
  ]);
}

剩下的工作就是将组件中的新操作拉入mapDispatchToProps并按下按钮进行调用。