133

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>  
);
3
  • 1
    Why do you want to reload the page? Commented Jan 5, 2017 at 9:41
  • @JyothiBabuAraja looking at the example, it looks like an error happens so prompting the user to reload isn't a bad idea. Commented Apr 17, 2018 at 20:31
  • @aug I don't think so. Instead of letting user in an error state, asking him for fresh start is fine. But ideally we should overcome such situations without user intervention. Commented Apr 18, 2018 at 7:01

7 Answers 7

275

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I actually refactored to use it as a button but now i get an error "Expected onClick listener to be a function, instead got type string"
<button type="button" onClick="refreshPage()"> <span>Reload</span> </button> function refreshPage(){ window.parent.location = window.parent.location.href; }
You have entered onClick value as string, use this: onClick={refreshPage} or onClick={() => refreshPage()}
45

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

2 Comments

this is wrong, because function will be called during render. Check comments under my answer
@LukasLiesis Thank you Just overlooked. Updated it
16

You can simply run this after successful query.

window.location.reload();

1 Comment

I would not recomended it if you want to preserve some state or have some logic based on that. You will lose it. Speaking in react context.
9

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

2 Comments

window.location.reload() is sufficient,for me the page didn't reload when tried window.location.reload(false)
this one worked as expected for my requirement
6

You can use this

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

Comments

1

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>

  );

}

Comments

0

My problem with window.location.reload(); is that it does not use client-side rendering. My solution was to do something like this:

// Empty.js
export default function Empty() {
    return (<></>);
}
// App.js
<Route path="refresh" element={<Empty />} />
// CustomLink.js
import { Link, useLocation, useNavigate } from "react-router-dom";

export default function CustomLink(props) {

    const location = useLocation();
    const navigate = useNavigate();

    return (
        <>
            <Link to={props.to} className={props.className} onClick={e => {
                e.preventDefault();
                if (location.pathname === props.to) {
                    navigate('refresh');
                    setTimeout(() => {
                        navigate(props.to);
                    }, 100);
                }
                else {
                    navigate(props.to);
                }
            }}>
                {props.children}
            </Link>
        </>
    );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.