React Iframe Component
react-iframe is a React component designed to simplify the integration of `<iframe>` elements into React applications. It acts as a convenient, TypeScript-supported wrapper around the native HTML `<iframe>` tag, abstracting common setup and attribute handling. The current stable version is 1.8.5. While it does not follow a strict release cadence, the project appears actively maintained with recent updates and comprehensive TypeScript support, making it well-suited for modern React development. Its key differentiator is providing a prop-driven interface for `iframe` attributes, including graceful handling of HTML5 deprecated attributes (like `frameBorder` and `allowFullScreen`), and offering explicit, convenient props for `sandbox` and `allow` attributes, which are crucial for security and feature control in contemporary web development and can be more cumbersome to manage directly with a plain `<iframe>` element.
Common errors
-
Refused to display 'https://example.com' in a frame because it set 'X-Frame-Options' to 'deny'.
cause The server hosting the embedded URL explicitly prevents it from being loaded in an iframe via the `X-Frame-Options` or `Content-Security-Policy` HTTP headers.fixContact the content provider to see if they allow iframing. If not, you cannot embed this specific URL directly. Consider alternative integration methods like APIs or direct links. -
Blocked a frame with origin "https://your-app.com" from accessing a cross-origin frame.
cause This error typically occurs when JavaScript within the iframe or the parent tries to interact with each other across different origins, violating the Same-Origin Policy, or when the `sandbox` attribute restricts such interactions.fixIf interaction is intended, ensure both origins are the same, or use `window.postMessage()` for secure cross-origin communication. If using `sandbox`, ensure `allow-same-origin` is included if you need the iframe to be considered same-origin, but be cautious of security implications. -
TypeError: Cannot read properties of undefined (reading 'default') OR Iframe is not a function
cause Occurs in CommonJS environments when attempting to `require('react-iframe')` without correctly accessing the default export.fixChange your import statement to `const Iframe = require('react-iframe').default;` to correctly import the default component. -
The 'sandbox' attribute has an invalid value 'invalid-value'.
cause The string or array provided to the `sandbox` prop contains one or more values that are not valid `iframe` sandbox tokens.fixReview the MDN documentation for `<iframe>` sandbox attribute to ensure all values are valid tokens (e.g., `allow-forms`, `allow-scripts`, `allow-same-origin`, etc.). Correct any misspelled or unrecognized values.
Warnings
- deprecated Several HTML iframe attributes like `frameBorder`, `scrolling`, and `allowFullScreen` are deprecated in HTML5. While `react-iframe` supports them for compatibility, it's recommended to achieve equivalent styling and functionality using CSS and the `allow` and `sandbox` attributes.
- gotcha The `sandbox` and `allow` attributes are critical for iframe security and permissions. Improper configuration, especially omitting `sandbox` or using overly permissive values, can expose your application to Cross-Site Scripting (XSS) or other vulnerabilities from the embedded content. Conversely, overly restrictive settings can prevent necessary functionality.
- gotcha The `styles` prop in `react-iframe` intentionally overrides any conflicting style-related props such as `width`, `height`, `position`, or `overflow`. This behavior can lead to unexpected sizing or positioning if not understood.
- gotcha Iframes are subject to browser security policies like the Same-Origin Policy, `X-Frame-Options` HTTP headers, and Content Security Policy (CSP). External content might refuse to load or interact if these policies are not met, often manifesting as blank iframes or console errors.
Install
-
npm install react-iframe -
yarn add react-iframe -
pnpm add react-iframe
Imports
- Iframe
import { Iframe } from 'react-iframe'import Iframe from 'react-iframe'
- Iframe
const Iframe = require('react-iframe')const Iframe = require('react-iframe').default - IframeProps
import type { IframeProps } from 'react-iframe'
Quickstart
import React from 'react';
import Iframe from 'react-iframe';
function MyPage() {
return (
<div>
<h1>My Embedded Content</h1>
<Iframe
url="https://www.sdrive.app/embed/1ptBQD"
width="640px"
height="320px"
id="my-sdrive-iframe"
className="my-iframe-class"
display="block"
position="relative"
frameBorder={0}
allow="fullscreen; camera; microphone; geolocation"
sandbox="allow-scripts allow-same-origin allow-forms allow-popups"
styles={{ border: '1px solid #ccc', borderRadius: '8px' }}
/>
<p>Content below the iframe.</p>
</div>
);
}
export default MyPage;