Created
June 26, 2019 22:36
-
-
Save justinobney/e062f846cf1b5addaace6821e1ef60f8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const usePostMessageHandler = (messageKey, callback) => { | |
useEffect(() => { | |
const receiveMessage = event => { | |
if (event.data === messageKey) { | |
callback(event); | |
} | |
}; | |
window.addEventListener("message", receiveMessage, false); | |
return () => { | |
window.removeEventListener("message", receiveMessage); | |
}; | |
}, [callback, messageKey]); | |
}; | |
usePostMessageHandler("completed-forms:refresh", event => { | |
setRefreshMarker(+new Date()); | |
}); | |
const NotifyParentWindow = ({ event }) => { | |
useEffect(() => { | |
window.onbeforeunload = () => { | |
if (window.opener) { | |
window.opener.postMessage(event); | |
} | |
}; | |
return () => { | |
window.onbeforeunload = undefined; | |
}; | |
}, [event]); | |
return null; | |
}; | |
<NotifyParentWindow event="completed-forms:refresh" />; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment