I have an HTML file with meta tags,header,body,forms coming from server in a string
Now my doubt is how to open that full fledged html in reactjs since we already have a master index.html in reactjs. When I tried to use the dangerouslySetInnerHTML it is giving a blank page.
-
did you check this: reactjs.org/docs/dom-elements.htmlAbu Sufian– Abu Sufian2020-03-02 07:04:35 +00:00Commented Mar 2, 2020 at 7:04
-
Yes I am using the dangerouslySetInnerHTML but my problem is a bit different. We already have our index.html file but from the server I am getting one payment gateway html which is something like this <html><head>....</head><body>....</body></html> . Now when I render this with dangerouslySetInnerHTML in a div its giving blank.user1295308– user12953082020-03-02 07:42:32 +00:00Commented Mar 2, 2020 at 7:42
-
You can render it in iframe but it's uglygadi tzkhori– gadi tzkhori2020-03-02 07:44:34 +00:00Commented Mar 2, 2020 at 7:44
-
Better if the server sends only json and client renders jsx.or you can turn your spa into ssrgadi tzkhori– gadi tzkhori2020-03-02 07:45:19 +00:00Commented Mar 2, 2020 at 7:45
-
do you have any errors in the console? you could select only the contents of the body of that html file and render that inside dangerouslySetInnerHTMLhiddenuser.2524– hiddenuser.25242020-03-02 07:45:21 +00:00Commented Mar 2, 2020 at 7:45
|
Show 4 more comments
1 Answer
As I mentioned in the comments you can do it in various ways. As much as I hate iframes it could be the right solution for you.
NOTE: this solution has nothing to do with React
import React from "react";
const html = `<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>div{
background: red;
color: white;
}</style>
</head>
<body>
<div>Hello iframe</div>
</body>
</html>`
export default function App() {
return (
<div className="App">
<iframe srcDoc={html} title="my-iframe"></iframe>
</div>
);
}
see live example: here
please read about iframes in MDN here for more config
more reading about polyfill to srcDoc: here
4 Comments
user1295308
Thanks it worked like charm .. but one more question how to read the response which the form in iframe is submitting. Right now it shows the jSON response in the page when the form is submitted. This is not expected. My main page should get some listener to get the event. The iFrame looks like this <body onload="return window.document.echoForm.submit()"> <form ....></form>
gadi tzkhori
You can use
postMessage event to pass data between the iframe and the main page developer.mozilla.org/en-US/docs/Web/API/Window/postMessagegadi tzkhori
I suggest you contact the support of this 3rd party solution you are using maybe they can help you. it seems like there's a gap here of how to implement it(on your side).
user1295308
Finally everything worked. I implemented a listener API for the payment integration. Once the listener receives success response I close the iframe. Thanks all for supporting. The solution of opening the link in iframe worked and also implementing the listener.