Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow postMessage communication #43

Open
conorot opened this issue Feb 11, 2019 · 8 comments
Open

Allow postMessage communication #43

conorot opened this issue Feb 11, 2019 · 8 comments

Comments

@conorot
Copy link

conorot commented Feb 11, 2019

It'd be great to be able to make use of HTML's postMessage API to enable cross origin comms between the iFrame and the parent page. Particularly useful for the use of a lot of Payment forms where you need to securely pass some info between your page and the iFrame.

Might require some re-tooling to work with DOM and iFrame loading. Perhaps it could be an option in the onLoad function?

https://javascript.info/cross-window-communication

@robbestad
Copy link
Owner

All right, will look at this next!

@jcvalerio
Copy link

jcvalerio commented Apr 16, 2019

@svenanders I was abe to implement this use case (@conorot) in previous versions
v1.5.0 to v1.6.2

But this is not supported anymore because since v1.7.0. I guest you migrate the implementation from
PureComponent to Stateless or Function Component

Since then this warning is fire by React
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()

And I'm not able to get a reference.

This works on v1.6.2 but not in 1.7.0

import React from 'react'
import { Button } from 'antd'

export default class DemoComponent extends React.PureComponent {
  formRef = {
    iFrame: null,
  }

  handleSubmit = () => {
    const { iFrame } = this.formRef
    if (iFrame && iFrame.refs) {
      const ref = iFrame.refs.iframe
      ref.contentWindow.postMessage('submit form', '*')
    }
  }

  saveIFrameRef = formRef => {
    this.formRef.iFrame = formRef
  }

  render() {
    return (
      <div>
        <Iframe
          url="SOME_URL"
          width="100%"
          height="600px"
          id="legacyEditor"
          display="initial"
          position="relative"
          allowFullScreen
          ref={this.saveIFrameRef}
        />
        <Button key="submit" type="primary" onClick={this.handleSubmit}>
          {'Send message to the IFrame'}
        </Button>
      </div>
    )
  }
}

@robbestad
Copy link
Owner

robbestad commented Apr 17, 2019 via email

@adtsoft
Copy link

adtsoft commented Jul 23, 2019

I have code that is very similar to what @jcvalerio is doing. I had to roll back to 1.6.2.

@hlolli
Copy link

hlolli commented Jul 24, 2019

I get Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

@adtsoft
Copy link

adtsoft commented Jul 24, 2019

@hlolli that is the problem we are discussing. If you use version 1.6.2 the above example from @jcvalerio will work.

@marcantoinepelletier
Copy link

Any chance to see this feature added directly into the package ? :)

@veerjainATgmail
Copy link

Does this work now? Any suggestions on alternatives? thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants