简体   繁体   English

未捕获的错误:调度不是函数

[英]Uncaught error: dispatch is not a function

I am using redux-saga along with reactjs. 我正在将redux-saga与reactjs一起使用。 I am using mapDispatchToProps to trigger onClick changes with values but I am getting above error saying that dispatch is not defined anyone please let me know how to resolve this in saga way? 我正在使用mapDispatchToProps来触发带有值的onClick更改,但是我遇到了以上错误,即未定义调度,请让我知道如何以传奇方式解决此问题?

import { connect } from 'react-redux';
import { updateModifiedValue } from "../actions/programActions";

import { bindActionCreators } from 'redux';

export class ProgramProfileContainer extends React.Component { 
 render() {

        return (
        <div>


             <ProgramVolumeComponent 
               updateModifiedGrowth = {(value) => updateModifiedValue(value,this.props.match.params.program_id)}

             />



            </div>
        )
    }
}
const mapStateToProps = (state) => {
    console.log("update state", state)

    return {
        programProfileData: state.programDetailReducer.programDetails,

    }
}
 const mapDispatchToProps= (dispatch) => (
  bindActionCreators({updateModifiedValue},dispatch)
 )
export default connect(
    mapStateToProps,
    mapDispatchToProps)(ProgramProfileContainer)

this is my action 这是我的行动

export function updateModifiedValue(Value,Id){
    console.log("mval",Value)
    console.log("id",Id)
    return {
        type:types.MODIFIED_GROWTH,
        growthValue
    }
}

here is my index saga 这是我的索引传奇

export default function* startForman() {
  console.log("index saga")
  yield fork(watcher);
}

watcher 守望者

export default function* watcher(){

    yield takeLatest(types.LISTS,myCallSaaga)

}

and my calll saaga 和我的call蒲

export default function* myCallSaaga({Value,Id}){
    const array=[Value,Id]

     try {
        const Data = yield call(growthdetail,...array)
        yield [
            put({ type: types.MODIFIED, Detail: Data })
        ]
    } catch (error) { 
        yield put({
            type: 'FETCH_ERROR',
            error
        });
    }
}

and my reducer 和我的减速器

export default function(state=[],action){
    switch(action.type){
        case types.PRO_LIST:
                return [...state,action.List]

        case types.MODIFIED:
    return {...state,growth:4}


        default:
        return state
    }
}

I think the error is that you are calling the action directly from your import and not from your props, which is what mapDispatchToProps is for. 我认为错误是您是直接从导入而不是从props调用操作,而mapDispatchToProps正是出于此目的。 Try this: 尝试这个:

 import programActionsCreator from "../actions/programActions";
 ...
 render() {
   const { updateModifiedValue } = this.props.actions;

   return() {
     ...
     updateModifiedGrowth = {(value) => updateModifiedValue(value, this.props.match.params.program_id)}
     ...
   }
  ...

 const mapDispatchToProps= (dispatch) => ({
   actions: bindActionCreators(programActionsCreator, dispatch)
 })

from rowland's comment and from this github post I made the changes like this 从rowland的评论以及此github帖子中,我进行了如下更改

const mapDispatchToProps= (dispatch) => {
return  { dispatch,
    ...bindActionCreators({
    updateModifiedValue
  }, dispatch)}//bindActionCreators({updateModifiedValue},dispatch)
}

and

<ProgramVolumeComponent 
               updateModifiedGrowth = {(value) => this.props.updateModifiedValue(value,this.props.match.params.program_id)}

             />

basically we need to pass our own dispatch we are mapDispatchToProps 基本上我们需要传递自己的调度,我们是mapDispatchToProps

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM