UNPKG

@nicholasadamou/react-iframe

Version:

A simple react component that lets you add headers to an iframe.

57 lines (48 loc) 1.45 kB
import React, { useEffect } from 'react'; function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } var Iframe = function Iframe(props) { var src = props.src; var iframe = React.createRef(); var method = 'GET'; var headers = new Headers(_extends({}, props.headers)); var options = { method: method, headers: headers }; var get = React.useRef(function () {}); get.current = function () { fetch(src, options).then(function (response) { return response.text(); }).then(function (response) { var frame = iframe.current.contentWindow || iframe.current.contentDocument.document || iframe.current.contentDocument; frame.document.open(); frame.document.write(response); frame.document.close(); })["catch"](function (e) { return console.log("@nicholasadamou/react-iframe error: " + e); }); }; useEffect(function () { get.current(); }, []); return /*#__PURE__*/React.createElement("iframe", _extends({ src: src, ref: iframe, title: "react-iframe" }, props)); }; export default Iframe; //# sourceMappingURL=index.modern.js.map