如何将参数传递到传奇中?

时间:2019-07-20 10:27:51

标签: reactjs react-redux redux-saga

在我的react组件中,我有:

import React from "react";
import "./App.css";
import { connect } from "react-redux";

function postOne(props, p) {
  debugger
  props.onRequestGet(p);
}

function App(props) {
  const { datas } = props;
  return (
    <div className="App">
      {datas}
      <button onClick={()=>postOne(props,1)}>post 1</button>
    </div>
  );
}

const mapStateToProps = state => {
  return {
    datas: state
  };
};

const mapDispatchToProps = dispatch => {
  return {
    onRequestGet: p => dispatch({ type: "API_GET_REQUEST", data: p })
  };
};

export default connect(
  mapStateToProps,
  mapDispatchToProps
)(App);

我正在尝试将参数p传递到actionrequest中。传奇代码看起来像这样:

import { takeLatest, call, put } from "redux-saga/effects";
import axios from "axios";

// how to pass in parameter here?
export function* watcherSaga(p) {
  yield takeLatest("API_GET_REQUEST", workerSaga,p);

}

// worker saga: makes the api call when watcher saga sees the action
function* workerSaga() {
  try {
    debugger
    const response = yield call(fetchGet);
    const post = response.data.author;

    // dispatch a success action to the store with the new dog
    yield put({ type: "API_GET_SUCCESS", post });
  } catch (error) {
    // dispatch a failure action to the store with the error
    yield put({ type: "API_CALL_FAILURE", error });
  }
}


// how to pass in parameter p?
function fetchGet(p) {
  debugger
  return axios({
    method: "get",
    url: "http://localhost:3000/posts/"+p
  });
}

我试图在watcherSaga函数中传递参数(即动作有效载荷),但没有结果。如何传递参数?

codesandbox

1 个答案:

答案 0 :(得分:1)

您在call()

中传递参数
const response = yield call(fetchGet, p)

https://redux-saga.js.org/docs/api/#callfn-args