1

I use react router in my website. I have homepage and contact.

When I am on homepage and I click on the homepage again. How can I reload the page like Facebook

issue here: https://github.com/ReactTraining/react-router/issues/1982

coinhndp
  • 2,281
  • 9
  • 33
  • 64

3 Answers3

0

After looking at the issue, I can realize that you may use force update:

componentDidUpdate(prevProps) {
    if (this.props.location !== prevProps.location) {
      this.forceUpdate();
    }
  }
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

You could set up an event handler on the 'home' Link, that will fetch the data you want to be updated.

      class Nav extends React.Component {
  constructor () {
    super()
    this.handleClick = this.handleClick.bind(this)
  }
  handleClick () {
    // code that fetches Home Page data
  }
  render () {
    return (
      <Link to='your/path' onClick={this.handleClick}>Home</Link>
    )
  }
}
adam.k
  • 622
  • 3
  • 15
  • Nav and Home is two different components so it does not work like that –  coinhndp Sep 24 '18 at 11:28
  • What kind of information are you trying to update on your home page and where is it stored? – adam.k Sep 24 '18 at 11:38
  • I use redux and action. It is a little bit weird if we put that login in Nav –  coinhndp Sep 24 '18 at 12:07
  • If your simply trying to dispatch an action to revert or update your home component. Give my suggestion a try, it should work. If not maybe have a look here https://stackoverflow.com/questions/38839510/forcing-a-react-router-link-to-load-a-page-even-if-were-already-on-that-page?rq=1 – adam.k Sep 24 '18 at 12:27
0

This might be a common problem and I was looking for a decent solution to have in my toolbet for next time. React-Router provides some mechanisms to know when an user tries to visit any page even the one they are already.

Reading the location.key hash, it's the perfect approach as it changes every-time the user try to navigate between any page.

componentDidUpdate (prevProps) {
    if (prevProps.location.key !== this.props.location.key) {
        this.setState({
            isFormSubmitted: false,
        })
    }
}

Reference: A location object is never mutated so you can use it in the lifecycle hooks to determine when navigation happens

Carlos Bensant
  • 289
  • 3
  • 7