Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use iframe in react.js

how can i require html from public/ with iframe?

my code like this:

 App.js

import React, {Component} from 'react';
import Iframe from './IframeComm';

class App extends Component {
    render() {
        return (
            <div className="App">
                <h1>react</h1>
                <Iframe
                    attributes={
                        {
                            src: '/test/test.html',
                            style: {
                                border: '10px solid red'
                            },
                        }
                    }
                />
            </div>
        );
    }
}

export default App;

test.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>
</head>
<body>
    iframe
</body>
</html>

when i usenpm start,everything allright

enter image description here

but when i use npm run build

enter image description here

toc:

enter image description here

how can i fixed this?thanks.

like image 300
liu Avatar asked Sep 14 '18 03:09

liu


People also ask

What is react iframe?

React Iframe. Simple React component for including an iframed page. The component is fully typescript-supported. Usage

What are iframes and how do you use them?

In a Nutshell, Iframes allow you to embed content from other websites into yours. When looking at the history, an “Inline frame” called Iframe was introduced in 1997 with HTML 4.01 by Microsoft Internet Explorer. First and foremost, let’s look at how to embed an Iframe in a React project.

How to prevent cross-site scripting attacks with react iframes?

Having a CSP is a great defense for your React app against cross-site scripting attacks. Unfortunately, there aren’t any restrictions we can enforce using CSP for the content loaded inside Iframes. However, a working draft by W3C allows the embedding site to propose a CSP for the Iframe by setting an attribute on it.

Does the parent component affect the iframe?

Therefore, neither the parent component’s CSS styling nor its JavaScript will have any effect on the iframe. In React, developers use iframes to create either a sandboxed component or an application that is isolated from its parent component.


1 Answers

You can simply create a new component that take source and return with Iframe;

iframe.js

import React from 'react';

const Iframe = ({ source }) => {

    if (!source) {
        return <div>Loading...</div>;
    }

    const src = source;     
    return (
        // basic bootstrap classes. you can change with yours.
        <div className="col-md-12">
            <div className="emdeb-responsive">
                <iframe src={src}></iframe>
            </div>
        </div>
    );
};

export default Iframe;

Then you can simply call that component like;

import React, {Component} from 'react';
import Iframe from './components/iframe.js';

class App extends Component {
    constructor(props) {
        super(props);
        this.state = {
            src: '/test/test.html'
        };
    }

    render() {
        return (
            <div className="App">
                <h1>react</h1>
                <Iframe source={this.state.src} />
            </div>
        );
    }
}

export default App;
like image 179
uedemir Avatar answered Oct 28 '22 16:10

uedemir