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>
);
This question is related to
javascript
reactjs
react-router
If you just put '/' in the href it will reload the current window.
<a href="/">
Reload the page
</a>
_x000D_
You can use this
<a onClick={() => {window.location.href="/something"}}>Something</a>
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
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.
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();
}
Source: Stackoverflow.com