2

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.

9
  • did you check this: reactjs.org/docs/dom-elements.html Commented 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. Commented Mar 2, 2020 at 7:42
  • You can render it in iframe but it's ugly Commented Mar 2, 2020 at 7:44
  • Better if the server sends only json and client renders jsx.or you can turn your spa into ssr Commented 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 dangerouslySetInnerHTML Commented Mar 2, 2020 at 7:45

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

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>
You can use postMessage event to pass data between the iframe and the main page developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
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).
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.

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.