简体   繁体   English

react-redux动作创建者在道具上不可用

[英]react-redux action creator not available on props

I am trying to use react-plaid-link to integrate plaid in to my app. 我正在尝试使用react-plaid-link集成格子到我的应用程序。 Everything works fine until I execute the handleOnSuccess callback. 一切正常,直到我执行handleOnSuccess回调。 I get the following error message: 我收到以下错误消息:

Uncaught TypeError: Cannot read property 'createTransferSource' of undefined 未捕获的TypeError:无法读取未定义的属性'createTransferSource'

For some reason my action creators, exported out of '../../actions' aren't available when I call handleOnSuccess 由于某些原因,当我调用handleOnSuccess时,从'../../actions'中导出的动作创建者不可用

This is my component 这是我的组成部分

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import PlaidLink from 'react-plaid-link';
import * as actions from '../../actions';

const plaidPublicKey = process.env.REACT_APP_PLAID_PUBLIC_KEY;

class PlaidAuthComponent extends Component {

  handleOnSuccess(token, metadata) {
    // send token to client server
    this.props.createTransferSource(metadata);  
  }

  render() {
    return (
      <div>
        <PlaidLink
          publicKey={`${plaidPublicKey}`}
          product="auth"
          env="sandbox"
          clientName="plaidname"
          onSuccess={this.handleOnSuccess}
          />
      </div>
    );
  }
}

export default connect(null, actions )(PlaidAuthComponent);

from my index.js file in my actions folder 从我的动作文件夹中的index.js文件中

export * from './transfer_actions';

my transfer_actions.js file 我的transfer_actions.js文件

export const createTransferSource = (values, callback) => {
  return function (dispatch){

  axios({
    method : 'POST',
    url: `xxx/transfer_source.json`, 
    data: { values } 
  })
    .then(response => {
      dispatch({ 
        type: CREATE_TRANSFER_SOURCE,
        payload: response
      });
    })
    .then(() => callback())
    .catch( error => {
    dispatch({
      type: CREATE_TRANSFER_SOURCE_ERROR,
      payload: error.response
    });
    });
  };
};

You need to bind this otherwise you would lose it in your component. 您需要绑定this否则您将在组件中丢失它。

You could do this in several ways: 您可以通过几种方式执行此操作:

Method One: 方法一:
Using arrow function 使用箭头功能

handleOnSuccess = (token, metadata) => {
  this.props.createTransferSource(metadata);
}

Method Two: 方法二:
Binding in the constructor. 绑定到构造函数中。

constructor(props) {
   super(props);

   this.handleOnSuccess = this.handleOnSuccess.bind(this); 
}

Method Three: 方法三:
Binding directly where handleOnSuccess was referenced. 直接绑定到引用handleOnSuccess位置。

render() {
    return (
      <div>
        <PlaidLink
          publicKey={`${plaidPublicKey}`}
          product="auth"
          env="sandbox"
          clientName="plaidname"
          onSuccess={this.handleOnSuccess.bind(this)}
          />
      </div>
   );
}

Method Four: 方法四:
Invoking handleOnSuccess reference with an arrow function 使用箭头功能调用handleOnSuccess参考

render() {
    return (
      <div>
        <PlaidLink
          publicKey={`${plaidPublicKey}`}
          product="auth"
          env="sandbox"
          clientName="plaidname"
          onSuccess={() => this.handleOnSuccess}
          />
      </div>
   );
}

I found the answer to this here: https://github.com/reactjs/react-redux/issues/328 我在这里找到了答案: https : //github.com/reactjs/react-redux/issues/328

I added the following code 我添加了以下代码

  constructor(props){
    super(props);
    this.handleOnSuccess = this.handleOnSuccess.bind(this); 
  }

You could write handleOnSuccess this way : 您可以这样编写handleOnSuccess

handleOnSuccess = (token, metadata) => {
  this.props.createTransferSource(metadata);
}

So that you preserve the this context and not require a constructor for the binding. 这样就可以保留this上下文,而不需要用于绑定的构造函数。

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

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