Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you reload an iframe with ReactJS?

Tags:

reactjs

iframe

My ReactJS component contains an iframe. In response to an event in the outer page, I need to reload the iframe. If the user has navigated to another page in the iframe, I need to reset it to the URL that it had when I first loaded the page. This URL is available in this.props.

I've tried using forceUpdate(). I can see that this causes the render method to run, but the iframe doesn't get reset - presumably because React can't tell that anything has changed.

At the moment I'm appending some random text to the iframe's querystring: this URL change forces React to re-render the iframe. However this feels a bit dirty: the page within the iframe is beyond my control so who knows what this extra querystring value might do?

resetIframe() {
    console.log("==== resetIframe ====");
    this.forceUpdate();
}

public render() {
    console.log("==== render ====");

    // How can I use just this.props.myUrl, without the Math.random()?
    let iframeUrl = this.props.myUrl + '&random=' + Math.random().toString();

    return <div>
        <button onClick={() => { this.resetIframe(); }}>Reset</button>
        <iframe src={iframeUrl}></iframe>
    </div>
}

(I'm using TypeScript too, if that makes a difference.)

like image 616
teedyay Avatar asked Feb 16 '18 15:02

teedyay


People also ask

How do I refresh an iframe?

To refresh an iframe using JavaScript, we call iframe. contentWindow. location. reload(); .

How do you reload in React JS?

Use reload() Method to Refresh a Page in React Manually Browsers natively provide a window interface with a location object, including the reload() method.

How do you check iframe is loaded or not in React?

To check if iframe is loaded or it has a content with JavaScript, we can set the iframe's onload property to a function that runs when the iframe is loaded. document. querySelector("iframe"). onload = () => { console.

Can we use iframe in Reactjs?

In React, developers use iframes to create either a sandboxed component or an application that is isolated from its parent component. In an iframe, when a piece of content is embedded from an external source, it is completely controlled by the source instead of the website it is embedded in.

Can you embed an iframe in a react app?

However, though it’s easy to embed an Iframe into your React app, making it secure, fast, and reliable requires specific expertise. Therefore, it’s essential to understand the best practices around using Iframes with React. Can We Add Any URL to SRC Attribute? As an experiment, I’ve used YouTube to embed a video into a React app.

How to check if a prop has changed in an iframe?

In your main component you load it and give it a update prop which you ignore in you iframe component. If you set the prop with a state variable, your component will reload. You can take a look at Diogo Sgrillo's answer for the setState method. You could also check with shouldcomponentupdate () if the prop changed and only update if it changed.

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.

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.


Video Answer


1 Answers

I'd create a state variable with the random, and just update it on resetIframe:

state = {
     random: 0
}
resetIframe() {
    this.setState({random: this.state.random + 1});
}

public render() {
    return <div>
        <button onClick={() => { this.resetIframe(); }}>Reset</button>
        <iframe key={this.state.random} src={this.props.myUrl}></iframe>
    </div>
}

Here is a fiddle working: https://codesandbox.io/s/pp3n7wnzvx

like image 52
Diogo Sgrillo Avatar answered Sep 17 '22 13:09

Diogo Sgrillo