Skip to content

redirect()

ts
function redirect(
   to: string | URL, 
   redirectQuery?: string, 
   redirectParam?: string): false;

Convenience function to redirect to a new path, replacing the current history entry.

For usage in routes enter hook, false is returned to prevent the default action of the event.

Parameters

to

string | URL

redirectQuery?

string

redirectParam?

string = 'redirect'

Returns

false

Examples

Redirect in a route guard:

ts
enter: () => {
  if (new URL(location.href).searchParams.has('secret')) {
    return redirect('/reveal-secret');
  }
  // ...
}

Redirect with query parameters:

ts
enter: () => {
  if (!isAuthenticated()) {
    const { pathname, search, hash } = location;
    const fromPath = `${pathname}${search}${hash}`;
    return redirect('/login', fromPath);
  }
  // ...
}