2

I am implementing a credit card payment form in ReactJS using a 3rd party bank JS which works using iframes.

The logic is I load bank's javascript in my page, something like this:

<script src="https://my-bank.com/super-secure-script.js"></script>

Then I collect user's CC data in my ReactJS component, then call a function like this:

window.Bank.SendPayment(CC, this.paymentCompletedCallback);

 ...

paymentCompletedCallback = (result) => {
  // process payment result here, inside my component
}

The problem is: the code inside super-secure-script.js cannot find the callback this.paymentCompletedCallback, because it is inside inside my component.

The question is: how can I pass to an external script a reference to a react object function?

10
  • 6
    I suppose Sendpayment is async ? Maybe this.paymentCompletedCallback.bind(this) solves the problem. Commented Sep 12, 2019 at 13:41
  • 1
    @Lumpenstein, using an arrow function automatically binds this in scope Commented Sep 12, 2019 at 13:43
  • 1
    @Claeusdev automatic this binding for fat arrow is a misconception (I believe), it dont have its own this thus bubbles up to use parent's this which in case of async gets lost and undefined most likely. Commented Sep 12, 2019 at 13:44
  • 1
    @MarcoS I tried creating tiny example of async and it seems working: stackblitz.com/edit/… Commented Sep 12, 2019 at 13:58
  • 1
    Thanks, everybody! I suppose the bind did the trick! I did never really understood that bind thing, sorry for wasting your time! If @Lumpenstein want to post his first comment a an answer, I will accept it... Many thanks to @Rikin, too! Commented Sep 12, 2019 at 14:13

2 Answers 2

2

You can attach the function to the window object:

const Component = () => {

  const func = () => {
    console.log('do something');
  };

  window.func = func;
};
Sign up to request clarification or add additional context in comments.

2 Comments

I think this simple solution should work, too... Didn't thing about it... :-( But the bind looks more like a react-thing... :-)
Bind is a JavaScript prototype. In the React Component world, if you organize your components into classes and need to change the state prop from a callback, you just bind your function to this. funA() { window._reactLoadingCB = this.ready.bind(this); } ready() { const { result } = this.state; console.log('if you have result set, this will work', result); }
1

this.paymentCompletedCallback.bind(this) may solve the problem.

Here is another example of when binding is necessary, hope that makes it more clear to you:

var Button = function(content) { 
  this.content = content;
};
Button.prototype.click = function() {
  console.log(this.content + ' clicked');
};

var myButton = new Button('OK');
myButton.click();

var looseClick = myButton.click;
looseClick(); // not bound, 'this' is not myButton - it is the global object

var boundClick = myButton.click.bind(myButton);
boundClick(); // bound, 'this' is myButton

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.