使用React定期重新加载iframe的最佳方法是什么? [英] What's the best way to periodically reload an iframe with React?
本文介绍了使用React定期重新加载iframe的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用React构建一个网页,这意味着我无法直接操作DOM。我已经尝试通过更新url状态重新加载我的iframe,但似乎没有重新加载页面。这是我尝试过的州代码。
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 the page. This is the state code I have tried.
componentDidMount: function() {
setInterval(function(){
this.setState({currentUrl:currentUrl});
}.bind(this), 1000);
},
getInitialState: function() {
return {defaultUrl:this.props.graph.url, currentUrl:this.props.graph.url};
},
render() {
// render the graph iframe
return (
<iframe src={this.state.currentUrl} id={this.props.graph.url} style={this.props.style} width={this.props.width} height={this.props.graph_height} />
)
}
推荐答案
更改组件
的关键属性,例如:
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}/>
这篇关于使用React定期重新加载iframe的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文