Redux-saga 如何将参数传递给 saga 内部的请求

Redux-saga how to pass parameters to request inside saga

我有一些代码需要更新。这不是我的代码,所以我不想做太多更改。它使用 redux-saga 和 axios。我想将一些参数传递给 saga

我在 sagas.js

的传奇
export function* getInfoSaga() {
    while (true) {
        yield take(mutations.GET_INFO);
        const url1 = `${url}/api/getInfo/1-100`;
        const logInfo = yield axios.get<any>(url1).then(response => response);
        yield put(mutations.setInfo(logInfo.data.data));
    }
}

mutations.js

export const GET_INFO = 'GET_INFO';

export const getInfo = () => ({
    type: GET_INFO,
});

传奇是 运行 index.js

const s = sagas as any;

for (const saga in s) {
    sagaMiddleware.run(s[saga]);
}

并且在某些文件中,操作是 运行 按钮和事件:

 onClick={() => dispatch({
                        type: 'GET_INFO',
                    })}

我想传递一些参数来修改请求 url,我尝试在 dipatch 请求中获取额外的对象并在 sagas.js 和 mutatuions.js 中向生成器添加参数,但是我得到了一个错误 无法访问未定义的属性

你试过了吗:

onClick={() => dispatch({
  type: 'GET_INFO',
  url: 'your/url/goes/here'
})}

export function* getInfoSaga(action) {
    while (true) {
        yield take(mutations.GET_INFO);
        const url1 = `${action.url}/api/getInfo/1-100`;
        const logInfo = yield axios.get<any>(url1).then(response => response);
        yield put(mutations.setInfo(logInfo.data.data));
    }
}

更新:传递有效负载并仅分配 'take'

中的 return 值
onClick={() => dispatch({
  type: 'GET_INFO',
  payload: {
    key1: value1
    // other values here
  }
})}

export function* getInfoSaga() {
   const action = yield take(mutations.GET_INFO);
   // action.payload.key1
}