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.

Add useRouter Hook

See original GitHub issue

Hi guys,

at current React Conf Hooks have been announced for React v16.7. What’s the plan for supporting them? Will there be a useRouter() any time soon?

https://reactjs.org/docs/hooks-faq.html#what-do-hooks-mean-for-popular-apis-like-redux-connect-and-react-router

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:231
  • Comments:62 (16 by maintainers)

github_iconTop GitHub Comments

146reactions
mjacksoncommented, Jul 9, 2019

We’re not making any progress here, so I’m going to close this. We will have a few hooks in the next major release, maybe even in a minor release. If you absolutely cannot wait, please be my guest and use some other router that ships with hooks.

81reactions
third774commented, Oct 31, 2018

You could also create your own wrapper to get access:

import React from "react";
import { BrowserRouter, Route } from "react-router-dom";

export const RouterContext = React.createContext({});

const CustomBrowserRouter = ({ children }) => (
  <BrowserRouter>
    <Route>
      {(routeProps) => (
        <RouterContext.Provider value={routeProps}>
          {children}
        </RouterContext.Provider>
      )}
    </Route>
  </BrowserRouter>
);

export default CustomBrowserRouter;

Then wrap this around your App instead of the normal <BrowserRouter />

import React from "react";
import CustomBrowserRouter from "./CustomBrowserRouter";

function App() {
  return (
    <CustomBrowserRouter>
      <div className="App">
        {/* The rest of your app  */}
      </div>
    </CustomBrowserRouter>
  );
}

export default App;

Then you can make a useRouter hook:

import {useContext} from "react"
import {RouterContext} from "./CustomBrowserRouter"

export default function useRouter() {
  return useContext(RouterContext)
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

useRouter React Hook
Hooks are a feature in React that allow you use state and other React features without writing classes. This website provides easy to...
Read more >
useRouter hook
React Hook API for react-router, realized with useContext. react. usecontext. router. hook. userouter ... Environmentcreate-react-app.
Read more >
useRouter
The useRouter hook is used for managing client-side routing and accessing browser history. Import. @getshogun/frontend-hooks is already pre-installed on every ...
Read more >
Add useRouter Hook · Issue #6430 · remix-run/react-router
Hi guys, at current React Conf Hooks have been announced for React v16.7. What's the plan for supporting them? Will there be a...
Read more >
Client Component Hooks: useRouter | Next.js
useRouter (). router.push(href: string) : Perform a client-side navigation to the provided route. Adds a new entry into the browser's history stack.
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