ReactJs:如何在不使用window.location.reload()的情况下加载/刷新/重新渲染组件



我想重新加载/刷新/重新渲染我的组件,但不使用‍‍‍window.location.reload()。有什么可能的办法吗?我已经尝试了this.forceUpdate())和this。setState({状态:这个。国家});但是这些对我的情况并没有帮助。

testDoneHandler(e) {
if( this.state && e.origin === document.location.origin ) {
if( e.data === 'testingClosed' ) {
this.setState({ state: this.state });

}
}
}

componentDidMount() {
window.addEventListener('message', this.testingClosed.bind(this));
}
任何建议/帮助都会很有帮助的。提前感谢

我想你需要这样的东西:

class t extends React.Component {
state = {
forceRerender: 0,
};
testDoneHandler(e) {
if (this.state && e.origin === document.location.origin) {
if (e.data === "testingClosed") {
this.setState((value) => ({ forceRerender: value.forceRerender + 1 }));
}
}
}
componentDidMount() {
window.addEventListener("message", this.testingClosed.bind(this));
}
console.log('forceUpdate', this.state.forceRerender);
}

最新更新