27

我在这个示例中使用 react-redux 和 redux-saga 进行 API 调用。我的目标是使用不同的 url 进行另一个 API 调用,并在不同的页面中使用它们。如何做到这一点?

传奇:

import { take, put,call } from 'redux-saga/effects';
import { takeEvery, delay ,takeLatest} from 'redux-saga';
function fetchData() {
    return  fetch("https://api.github.com/repos/vmg/redcarpet/issues?state=closed")
    .then(res => res.json() )
    .then(data => ({ data }) )
    .catch(ex => {
        console.log('parsing failed', ex);
        return ({ ex });
    });
}
function* yourSaga(action) {
    const { data, ex } = yield call(fetchData);
    if (data)
    yield put({ type: 'REQUEST_DONE', data });
    else
    yield put({ type: 'REQUEST_FAILED', ex });
}
export default function* watchAsync() {
    yield* takeLatest('BLAH', yourSaga);
}
export default function* rootSaga() {
    yield [
        watchAsync()
    ]
}

应用程序:

import React, { Component } from 'react';
import { connect } from 'react-redux';
class App extends Component {
    componentWillMount() {
        this.props.dispatch({type: 'BLAH'});
    }
    render(){
       return (<div>
            {this.props.exception && <span>exception: {this.props.exception}</span>}
            Data: {this.props.data.map(e=><div key={e.id}>{e.url}</div>)}

          </div>);
    }
}
export default connect( state =>({
    data:state.data , exception:state.exception
}))(App);

我的目标是制作另一个传奇,我将在另一个组件中使用它,并且两者都不会互相干扰。这可能吗?

4

3 回答 3

31

Redux Saga 使用all最新版本(0.15.3)中的功能将多个 saga 组合成一个根 saga 用于 Redux 存储。

import { takeEvery, all } from 'redux-saga/effects';

...

function *watchAll() {
  yield all([
    takeEvery("FRIEND_FETCH_REQUESTED", fetchFriends),
    takeEvery("CREATE_USER_REQUESTED", createUser)
  ]);
}

export default watchAll;

在 Redux 商店中,您可以将根 saga 用于 saga 中间件:

import { createStore, applyMiddleware } from 'redux';
import createSagaMiddleware from 'redux-saga';

import rootReducer from './reducers';
import rootSaga from './sagas';

const sagaMiddleware = createSagaMiddleware();
const store = createStore(
  rootReducer,
  applyMiddleware(sagaMiddleware)
);

sagaMiddleware.run(rootSaga)
于 2017-06-10T15:22:52.753 回答
23

当然,这就是 sagas 的全部意义所在。

一个典型的应用程序会有多个 sagas 在后台等待,等待特定的动作/动作(take效果)。

下面是如何从redux-saga issue#276设置多个 saga 的示例:

./saga.js

function* rootSaga () {
    yield [
        fork(saga1), // saga1 can also yield [ fork(actionOne), fork(actionTwo) ]
        fork(saga2),
    ];
}

./main.js

import { createStore, applyMiddleware } from 'redux'
import createSagaMiddleware from 'redux-saga'

import rootReducer from './reducers'
import rootSaga from './sagas'


const sagaMiddleware = createSagaMiddleware()
const store = createStore(
  rootReducer,
  applyMiddleware(sagaMiddleware)
)
sagaMiddleware.run(rootSaga)
于 2016-09-23T18:12:50.067 回答
6

自从发布最后一个答案以来,这已经发生了一些变化。如https://redux-saga.js.org/docs/advanced/RootSaga.html所述,创建根 saga 的首选方法是使用spawn

export default function* rootSaga() {
  yield spawn(saga1)
  yield spawn(saga2)
  yield spawn(saga3)
}

spawn是一种将您的子 saga 与其父级断开连接的效果,允许它在不崩溃其父级的情况下失败。这仅仅意味着即使一个 saga 失败,另一个 sagarootSaga也不会被杀死。还有一种方法可以恢复失败的 sagas(更多信息可在上面提到的链接中找到)。

于 2020-02-25T11:05:54.017 回答