How to refresh a Page using react-route Link

JavascriptReactjsReact Router

Javascript Problem Overview


I am trying to refresh a page using react-route Link. But the way I have implemented it goes to the URL one step back.(as an example if the URL was ../client/home/register and when I press the reload is goes to ../client/home)

below is my code

const AppErrorPage = () => (
    <div>
    <div style={ styles.container }>
        <h2>Error</h2>
        <p> Something went wrong, please reload the page </p>
        <div>
         <Link to="" refresh="true">
            <span>Reload</span>
          </Link>
        </div>
    </div>
    </div>  
);

Javascript Solutions


Solution 1 - Javascript

To refresh page you don't need react-router, simple js:

window.location.reload();

To re-render view in React component, you can just fire update with props/state.

Solution 2 - Javascript

Try like this.

You must give a function as value to onClick()

You button:

<button type="button" onClick={ refreshPage }> <span>Reload</span> </button> 

refreshPage function:

function refreshPage(){ 
    window.location.reload(); 
}

Solution 3 - Javascript

You can use this

<a onClick={() => {window.location.href="/something"}}>Something</a>

Solution 4 - Javascript

I ended up keeping Link and adding the reload to the Link's onClick event with a timeout like this:

function refreshPage() {
	setTimeout(()=>{
		window.location.reload(false);
	}, 500);
	console.log('page to reload')
}

<Link to={{pathname:"/"}} onClick={refreshPage}>Home</Link>

without the timeout, the refresh function would run first

Solution 5 - Javascript

Here's one way of doing it using React Bootstrap and a component that you can then drop into any page...

import Button from 'react-bootstrap/Button';

export const RefreshCurrentPage = () => {

  function refreshPage(){ 
    window.location.reload(); 
  }

  return (
    <div className="row">
      <Button onClick={ refreshPage } variant="secondary" size="sm" className="btn ml-4">Refresh Page</Button>
    </div>

  );

}

Solution 6 - Javascript

You can simply run this after successful query.

window.location.reload();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnnaView Question on Stackoverflow
Solution 1 - JavascriptLukas LiesisView Answer on Stackoverflow
Solution 2 - JavascriptJyothi Babu ArajaView Answer on Stackoverflow
Solution 3 - JavascriptFerdousiView Answer on Stackoverflow
Solution 4 - Javascriptsigmapi13View Answer on Stackoverflow
Solution 5 - JavascriptDavid BarrowsView Answer on Stackoverflow
Solution 6 - JavascriptGaurav KumarView Answer on Stackoverflow