-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
122 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React, { useRef, useEffect } from 'react' | ||
|
||
const PostMessage = () => { | ||
const origin = process.env.IFRAME_ORIGIN | ||
const childOrigin = `${process.env.IFRAME_ORIGIN}/#/my-iframe` | ||
const ref = useRef() | ||
|
||
const onReceivedMessage = (event) => { | ||
if (event.origin !== childOrigin) { | ||
// do something with the received messages | ||
} | ||
|
||
// do something with the received messages | ||
} | ||
|
||
const sendMessage = () => { | ||
if (!ref.current) { | ||
return | ||
} | ||
ref.current.contentWindow.postMessage(`Hello iframe!${Math.random()}`, origin) | ||
} | ||
|
||
useEffect(() => { | ||
window.addEventListener('message', onReceivedMessage) | ||
|
||
return () => { | ||
window.removeEventListener('message', onReceivedMessage) | ||
} | ||
}) | ||
|
||
return ( | ||
<> | ||
<button onClick={sendMessage}>send a message</button> | ||
|
||
<iframe | ||
style={{ | ||
display: 'block', | ||
border: 'none', | ||
width: '100%', | ||
}} | ||
src={childOrigin} | ||
ref={ref} | ||
width="800px" | ||
height="600px" | ||
/> | ||
</> | ||
) | ||
} | ||
|
||
export default PostMessage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { useState, useEffect } from 'react' | ||
|
||
const MyIFrame = () => { | ||
const parentOrigin = 'http://localhost:8080' | ||
const [message, setMessage] = useState() // string | undefined | ||
|
||
const onReceivedMessage = (event) => { | ||
// check the message source origin as a security measure | ||
if (event.origin !== parentOrigin) { | ||
return | ||
} | ||
|
||
// see notes on checking the data type | ||
setMessage(event.data) | ||
} | ||
|
||
useEffect(() => { | ||
window.addEventListener('message', onReceivedMessage) | ||
|
||
return () => { | ||
window.removeEventListener('message', onReceivedMessage) | ||
} | ||
}) | ||
|
||
if (!message) { | ||
return <p>no message has been received</p> | ||
} | ||
|
||
return <p>message received: {message}</p> | ||
} | ||
|
||
export default MyIFrame |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters