React-router: how to reload current page

Created on 17 Oct 2016  路  9Comments  路  Source: ReactTraining/react-router

i want to reload the current page after updating some data in form , so i write like this:
browserHistory.push('current page url'), but i find that it doesnt work .

Most helpful comment

i use window.location.reload() , the data will update

All 9 comments

That will replace the current url, which will re-render your Route components. What do you mean specifically by it 'doesn't work'?

I don't think this is a react-router issue, as pushing a url is very well tested. Closing for now until there's more info.

@alisd23 i mean that my data doesn't update when i browserHistory.push('current page url')

@alisd23 it re-rendered my Route component, but the data that i updated show like before, i think it maybe cookie problem

If the Route component re-rendered then it's doing its job properly :+1:
How you handle state in those route components is nothing to do with the router.

i use window.location.reload() , the data will update

window.location.reload - is reloading your entire page - ie refetching the page from the server and starting again from scratch

I was looking for the same solution, being able to completely reload the page/browser as if the (physical) user was visiting it for the first time. I needed this because I wanted to clear down all memory/state/etc from the page when a user logged out so nothing is leaked.

You can use a refresh route.

const Refresh = ({ path = '/' }) => (
    <Route
        path={path}
        component={({ history, location, match }) => {
            history.replace({
                ...location,
                pathname:location.pathname.substring(match.path.length)
            });
            return null;
        }}
    />
);

Usage:

<Refresh path="/refresh"/>

Now you can push to history with the url prefixed with /refresh and it will rerender the route (this happens because Refresh route uses a component which forces remount of component) .

history.push('/refresh/') => Reloads /
history.push('/refresh/posts?user=tkvw') => Reloads /posts?user=tkvw

The /refresh/ urls won't be visible in the history because they are replaced.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ArthurRougier picture ArthurRougier  路  3Comments

tomatau picture tomatau  路  3Comments

nicolashery picture nicolashery  路  3Comments

ryansobol picture ryansobol  路  3Comments

jzimmek picture jzimmek  路  3Comments