javascript - Redux-saga如何向saga内部请求传递参数

标签 javascript reactjs typescript react-redux redux-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 中向生成器添加参数,但我收到错误 cannot access prperty of undefined

最佳答案

你尝试过吗:

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

关于javascript - Redux-saga如何向saga内部请求传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60800404/

相关文章:

javascript - 从 JSON 对象中减去数组 JavaScript

javascript - 如何读取 Button 组件的 propTypes 的值

javascript - React Redux MapStateToProps

typescript - TypeScript 编译器不使用 tsconfig.json?

jquery - 转向 Typescript,数字错误

javascript - HTML 图像有条件下载

javascript - 保护 javascript [AJAX] 代码?

ReactJS : In react-rooter-dom, 是否已弃用 useHistory?

reactjs - typescript :React.forwardRef 带有来自@material-ui 的通用 Prop

php - Ajax 更新带有 id 的 div