npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-router-use-history

v1.1.0

Published

useHistory api in react router v6

Downloads

375

Readme

react-router-use-history

useHistory in react router v6

Install

  pnpm add react-router-use-history

Usage

Router created with createBrowserRouter and <RouterProvider /> (Data Browser Router)

React router >= v6.4.0

import { useHistory } from 'react-router-use-history'

function Page() {
  const history = useHistory()
  // ...
  history.push('/a')
}

Router created with <BrowserRouter /> (Legacy Browser Router)

React router >= v6.0.0

  1. Replace BrowserRouter :

    +import { BrowserRouter } from 'react-router-use-history'
    -import { BrowserRouter } from 'react-router-dom'
    
    function Root() {
      return (
        <BrowserRouter>
          {/* ... */}
        </BrowserRouter>
      )
    }
  2. Enjoy useHitory :

    import { useHistory } from 'react-router-use-history'
    
    function Page() {
      const history = useHistory()
      // ...
      history.push('/a')
    }

Advanced usage

useHistorySelector

history selector for deep selection value

import { useHistorySelector } from 'react-router-use-history'

function Page() {
  const pathname = useHistorySelector(h => h.location.pathname)
}

Custom history outside of react router

You can define your own history outside of the react router :

  pnpm add history
  1. Create independent history

    // history.ts
    import { createBrowserHistory } from 'history'
    
    export const history = createBrowserHistory()
    
    // or
    // import { createBrowserHistory } from '@remix-run/router'
    // export const history = createBrowserHistory({ v5Compat: true })
  2. Replace <BrowserRouter> and inject history instance

    import { BrowserRouter } from 'react-router-use-history'
    import { history } from './history'
    
    function Root() {
      return (
        <BrowserRouter history={history}>
          {/* ... */}
        </BrowserRouter>
      )
    }
  3. Then you can use history anywhere

    // anywhere.ts
    import { history } from './history'
    
    history.push('/page')

License

MIT