簡體   English   中英

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));
    }
}

突變.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 中的生成器但是我得到一個錯誤,無法訪問未定義的 prperty

你有沒有嘗試過:

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”分配返回值

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM