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.

How to redirect to another route programmatically

See original GitHub issue

I have an <input /> with a handler on ‘enter’ key up event handler. How can I redirect to /app?keyword=[input_value] in that event handler.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:2
  • Comments:27 (6 by maintainers)

github_iconTop GitHub Comments

45reactions
davedxcommented, May 20, 2016

This is crazy, the documentation is terrible when describing something as basic as navigating programmatically. The link you post still doesn’t make sense to me, my component gets a ‘history’ not a ‘router’ passed into its props (v2), so I still have no idea from the docs how to do basic navigation in my code.

If the documentation was better people wouldn’t need to comment on old Github issues. So frustrating.

21reactions
TeemuKoivistocommented, Jun 16, 2016

Say what you want but the docs are bad and confusing. To stop the user from ever even visiting a page thus not causing any unwanted API-calls happening the best method I found is to put a middleware function to route’s onEnter -property and check if user is logged in / has required privileges. Not so easy to just “read the docs” and know what to do. So I have route like this:

<Route path="user/me" component={UserShow} onEnter={redirectNonUser} />

And then middleware function like this:

export const redirectNonUser = (nextState, replace) => {
  const user = store.getState().get("auth").get("user").toJS();
  if (user.role === undefined) {
    replace({
      pathname: "/login",
    });
  }
};

Which works but was way too hard to figure out and I think the hackiness still shows and it still feels to me a bit ambiguous

Read more comments on GitHub >

github_iconTop Results From Across the Web

Programmatically navigate using React router - Stack Overflow
Rendering a <Redirect> will navigate to a new location. The new location will override the current location in the history stack, like server- ......
Read more >
Programmatically Navigate with React Router - Telerik
The primary way you programmatically navigate using React Router v4+ is by using a <Redirect /> component, and it's a recommended method that ......
Read more >
Programmatic Navigation in React using react-router
We can redirect using <Redirect> component by simply passing the route we want to redirect to and rendering the component. It already comes ......
Read more >
Programmatically navigate with React Router (and Hooks)
How to programmatically navigate with React Router v6 and the new useNavigate hook. ... Redirect Default or 404 Routes with React Router.
Read more >
Redirecting the Router to Another Route - Rangle.io
The pathMatch property, which is required for redirects, tells the router how it should match the URL provided in order to redirect to...
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