使用redux-saga时,参数不会从组件传递到操作

时间:2018-03-28 12:43:43

标签: reactjs react-redux redux-saga

我正在使用Redux-saga,以便我可以从API端获取项目。我已根据http://www.utilities-online.info/xsltransformation/#.WrtA4S5uZEQ中给出的指令在索引页面中初始化了我的saga中间件。

以下代码段是我的/src/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import createSagaMiddleware from 'redux-saga';
import reducer from './reducers';
import App from './root-components/app';

export const sagaMiddleware = createSagaMiddleware()
const store = createStore(reducer, applyMiddleware(sagaMiddleware));

ReactDOM.render(
  <Provider store={store}>
    <div>
      <App />
    </div>
  </Provider>,
  document.getElementById('root')
);

我的组件是src/component/index.jsx

import React, { Component } from 'react';
import { sagaMiddleWare } from '../index.js';
import { fetchPrescriptionsFromUrl } from '../actions/presAction';

class Index extends Component {
  componentWillMount(){
    const res = "434566ytui87unhygt99";
    sagaMiddleware.run(fetchPrescriptionsFromUrl);
    fetchPrescriptionsFromUrl(res); 
  }

  render(){
    return(
      <div>
        <h1> My First Component </h1>
      </div>
    )
  }
}

export default Index;

我正在调用操作fetchPrescriptionsFromUrl(res)并传递参数res。我的操作是presAction

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

const getCharacters = (accessPoint) => {
  axios.get(accessPoint)
}

export function *fetchPrescriptionsFromUrl(accessToken){
  console.log('AT', accessToken)
  try {
    const response = yield call(getCharacters(accessToken))
  } catch (e) {
    console.log(e)
  }
}

console.log组件传递的index.jsx参数声明为undefined

我是redux-saga的初学者,不知道在哪里犯错误。如果我有任何解决方案可以解决这个问题。

1 个答案:

答案 0 :(得分:0)

Redux Saga调用函数不能用这种方式。看起来应该是这样的

getCharacters

在你的情况下,call被imidiatelly评估,结果传递到# Here I apply the rotation matrix (this will become a function one day) a1 = vector[0] * x[0,0] + vector[0] * x[0,1] + vector[0] * x[0,2] b1 = vector[1] * x[1,0] + vector[1] * x[1,1] + vector[1] * x[1,2] c1 = vector[2] * x[2,0] + vector[2] * x[2,1] + vector[2] * x[2,2]

此外,您不应在索引组件中使用sagaMiddleWare。 (不仅仅是传奇)中间件的一点是,您可以正常发送操作,然后在中间件中捕获具体操作