question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

It would be nice to be able to setup redirects via a useRedirect hook or similar mechanism.

I expect the router to navigate the user to the target path (preserving the queryString if present) and replacing the navigation history with the destination path.

Basic example

import {useRoutes, useRedirect} from 'raviger';

const routes = {
  '/dashboard': () => 'Dashboard'
};

const MyApp = () => {
  useRedirect('/', '/dashboard);
  return useRoutes(routes) || 'oops';
}

I’d expect the user to end up in the /dashboard page when hitting the root of the App and having only /dashboard in its History.

Extra Could be interesting to think about merging queryParameters defined in the useRedirect hook configuration with the ones provided in a Link. For example I could setup a redirect such as

  useRedirect('/', '/dashboard?redirected=1');

Or

  useRedirect('/', '/dashboard', {redirected: 1});

And then later in a hypothetical SomePage component make a Link to go back to the Dashboard

  <Link href='/?from='some-page''>Go back</Link>

In this case I’d expect the queryParameters to contain both redirected and from

But this is already quite advanced, I’d be very happy already with the Basic functionality 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sdirosacommented, Aug 22, 2019

Anonymous users can basically access only the login page, which sends them to a 3rd party authentication layer.

If thats the case, you can skip the redirect and just return the <Login> component when they aren’t logged in. If the whole app is authenticated you can put this at the top level, above the router.

Indeed. For login it would work fine. For other pages not so much; it would leave the url out of sync or create multiple routes for the same component.

Eg: Overview would render both under / and /overview. useRedirect fixes the problem entirely 😃

0reactions
kyeoticcommented, Aug 22, 2019

Anonymous users can basically access only the login page, which sends them to a 3rd party authentication layer.

If thats the case, you can skip the redirect and just return the <Login> component when they aren’t logged in. If the whole app is authenticated you can put this at the top level, above the router.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Proposal] useRedirect hook · Issue #6932 - GitHub
Current situation To perform redirects inside an application, react-router exposes a Redirect component. The component works pretty well, ...
Read more >
useRedirect - CodeSandbox
CodeSandbox is an online editor tailored for web applications.
Read more >
useRedirect - React-admin - Marmelab
useRedirect. This hook returns a function that redirects the user to another page. import { useRedirect } from 'react-admin'; const DashboardButton ...
Read more >
useHistory hook - React Router: Declarative Routing for React.js
The useRouteMatch hook attempts to match the current URL in the same way that a <Route> would. It's mostly useful for getting access...
Read more >
React's useHistory and Redirect hooks - DEV Community ‍ ‍
This tutorial is based on exploring the useHistory hook of react-router-dom which is a special package of react that allows us to make...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found