【发布时间】:2017-01-10 19:26:50
【问题描述】:
我正在使用 React 构建网页,这意味着我无法直接操作 DOM。我尝试通过更新 url 状态来重新加载我的 iframe,但这似乎并没有重新加载页面。这是我尝试过的状态代码。
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} />
)
}
【问题讨论】:
-
使用
forceUpdate会强制重新渲染():facebook.github.io/react/docs/react-component.html#forceupdate -
我试过了,但它没有做任何事情 iframe 仍然没有重新加载
-
定义“没有做任何事情”。对
forceUpdate的调用保证会在您的组件中调用render,这必然会重新绘制iframe。你能创建一个 plunkr 或 codepen 吗? -
不是强制更新,可能是我用错了。我刚刚替换了 this.setState({currentUrl:currentUrl});用 this.forceUpdate()
-
那支笔对我有用!如果您将
console.log('Rendering');放在render()函数的顶部(return之前),您会看到它实际上会重新渲染。也许 iframe 本身的内容没有改变?但这将在 React 之外。
标签: javascript reactjs iframe