

In addition to that, the dangerouslySetInnerHTML attribute exposes your visitors to XSS attacks. The page in the iframe might take too long to load, which can also affect the performance of the entire page. The problem is that you don’t control the source page, and it could display anything. In general, putting iframe in your React applications is considered an unsafe practice. the Effect of Putting iframe in React Applications It’s always better to avoid using the dangerouslySetInnerHTML attribute for safety reasons described below. Sometimes you’ll have to parse the string to extract the values like height, width, and source for the iframe tag. You may use this component anywhere in your application, as long as you have the necessary values to pass down as props. This attribute’s value should be an object with _html key and the string HTML value.Įxport default function Component(props) = this.props The easiest way to insert an iframe into your React application is to use the dangerouslySetInnerHTML attribute. For instance, it can be an object with properties corresponding to these values. It’s a bit easier if the data received from the server has more structure and specifies the width, height, and source of an iframe. The most difficult part of inserting an iframe into your React application is parsing the string value to get its properties, such as source, width, and height. Most of the time, it is formatted as a string.
React iframe code#
It’s not uncommon to get a pre-structured HTML code as a response from the server. In this article, we’ll discuss the case of inserting an iframe tag into your application. In some cases, you need to inject an HTML code into your existing markup. Use dangerouslySetInnerHTML Attribute to Display Iframe in React In response to an event in the outer page, I need to reload the iframe. The syntax of JSX looks a lot like HTML, so it’s easy to write even if you’re completely new to React. React developers use JSX to structure the markup of their applications easily. the Effect of Putting iframe in React ApplicationsĮvery modern application needs an HTML structure.Create a Custom Component for Iframe in React.Use dangerouslySetInnerHTML Attribute to Display Iframe in React.
