What's the best way to periodically reload an iframe with React?

后端 未结 2 1771
一整个雨季
一整个雨季 2021-01-23 18:28

I\'m building a webpage using React which means I can\'t manipulate the DOM directly. I\'ve tried reloading my iframe by updating the url state but that doesn\'t seem to reload

相关标签:
2条回答
  • 2021-01-23 18:44

    Change key property for component for example:

    this.state = {iframeKey: 0};
    
    setInterval(() => this.setState(s => ({iframeKey: s.iframeKey + 1})), 1000);
    
    <Iframe key={this.state.iframeKey} url={some url}/>
    
    0 讨论(0)
  • 2021-01-23 18:53

    This is a use case for the ref property if, as I assume, the contents of the iframe isn't under react control. Apply one to the iframe, then use the setInterval to refresh the iframe url.

    The reason the iframe isn't updating when you set the URL in state is that the url hasn't changed(?) and react doesn't redraw when that's the case.

    0 讨论(0)
提交回复
热议问题