【问题标题】:Calling actionCreator function is not working through bindActionCreators调用 actionCreator 函数不能通过 bindActionCreators 工作
【发布时间】:2018-10-28 18:05:05
【问题描述】:

我正在用下面的 actionCreator 附加dispath

function mapDispatchToProps(dispatch) {

  return {
    actions: bindActionCreators(Object.assign({},basicActions), dispatch)
  }
}

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

并像这样调用 actionCreator 函数

componentCallback:text => actions(basicActions.selectOption(text))

这是 basicActions.js

export function selectOption(text){
    debugger;
    const actionObj = {
        type: types.SELECT_OPTION,
        id: nextActionId++,
        text
    };
    return actionObj;
}


export function requestSuccess(){
    console.log('inside requestSuccess');
    return {type:'SUCCESS',id:1}
}

export function fetchS1Users(parentUserId){
debugger;
    axios.get(url, {
        params: {
          parentUserVal: parentUserId
        },
        responseType: 'json'
      })
      .then(function (response) {
        console.log('res '+response.data);
        console.log('res JSON '+JSON.stringify(response.data));
      })
      .catch(function (error) {
        console.log(error);
      });

    return {
        id: nextActionId++,
        type: types.FETCH_USERS,
        inputParam: parentUserId,
        s1users:{}
    }
}

但它给出的错误'Uncaught TypeError:actions is not a function'

这确实有效

 actions: bindActionCreators(basicActions.selectOptions, dispatch)

【问题讨论】:

    标签: reactjs redux redux-thunk


    【解决方案1】:

    使用bindActionCreators 绑定动作后,您可以从道具中使用它们,而不是像函数那样调用动作

    this.props.actions.selectOption()

    【讨论】:

    • thanx,但这对我来说不起作用。现在我这样打电话和它的工作this.props.actions(basicActions.selectOptions('abc'))
    猜你喜欢
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    相关资源
    最近更新 更多