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-ad-hocs

v0.9.2

Published

Higher-order components that do what react-router's mixins do

Downloads

17

Readme

react-router-ad-hocs

This is a set of three higher-order components that do (more-or-less) the same things as react-router's mixins. You can use these with React components that you've defined as ES6 classes or with those fancy, stateless, functional components that all the kids are talking about.

Here's how they work:

import React, { Component } from 'react'
import { connectHistory } from 'react-router-ad-hocs'

class MySpecialLink extends Component {
  handleClick () {
    this.props.history.pushState(null, '/foo')
  }
  render () {
    return (
      <h1 onClick={() => this.handleClick()}>
        Click Me!
      </h1>
    )
  }
}

export default connectHistory(MySpecialLink)

API

connectHistory(ComponentToBeWrapped)

Parameters:

ComponentToBeWrapped: Any React component

Returns:

A wrapped version of ComponentToBeWrapped that will receive react-router's history object as a prop. See the History mixin documentation for more information. If you are already passing a prop called history yourself, it will be overwritten by connectHistory(), so don't do that.

connectLifecycle(ComponentToBeWrapped)

Parameters:

ComponentToBeWrapped: Any React component that defines a routerWillLeave method

Returns:

A wrapped version of ComponentToBeWrapped. ComponentToBeWrapped's routerWillLeave method will be called immediately before the router leaves the current route, with the ability to cancel the navigation. See the Lifecycle mixin documentation for more information.

connectRouteContext(ComponentToBeWrapped)

Parameters:

ComponentToBeWrapped: Any React component that receives route as a prop (i.e. a Route component)

Returns:

A wrapped version of ComponentToBeWrapped that provides route on context for all its children. You'd use this on Route components whose children want to use connectLifecycle() because connectLifecycle() needs to know about the current route. See the RouteContext mixin documentation for more information.

Caveats and differences from react-router mixins:

  1. connectHistory() passes history as a prop, whereas the History mixin adds it directly to your component definition and makes it accessible through this.history. I decided to pass it as a prop because props are more easily composable. It's probably not a big deal either way, so I guess let me know if this is a problem for you.

  2. If you're using connectHistory() and connectLifecycle() together on the same component, it's important that connectLifecycle() be applied first (i.e. receive the component first in the composition chain) because it needs to directly access the unwrapped component's .routerWillLeave() method, which will otherwise be obscured by connectHistory(). So do this:

const WrappedComponent = connectHistory(connectLifecycle(UnwrappedComponent))

not this:

const WrappedComponent = connectLifecycle(connectHistory(UnwrappedComponent))

That should cover everything. Have fun!