Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

ReactJs: How to Reload/refresh/re-render component without using window.location.reload()

I want to reload/refresh/re-render my component but without using ???window.location.reload(). Is there any possible way to do so. I have tried with this.forceUpdate() and this.setState({ state: this.state }); but these are not actually helping me in my case.

 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));
  }

Any suggestion/help would be helpful.Thanks in advance

question from:https://stackoverflow.com/questions/65884076/reactjs-how-to-reload-refresh-re-render-component-without-using-window-location

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think you need something like 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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...