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

ctrl-react-router

v3.1.0

Published

An alternative way of handling routing in React.

Downloads

20

Readme

ctrl-react-router

Build Status Codacy Badge Codacy Badge

An alternative way of handling routing in React.

Installation

Yarn is recommended for installation.

$ yarn add --dev ctrl-react-router

But you can still use npm:

$ npm install --save-dev ctrl-react-router

Props

log - Takes an optional debug logger.

Example:

<CtrlReactRouter log={debug('my-debug:router')}/>

Context Properties and Methods

CtrlReactRouter use React's getchildContext method to pass usable info and actions to its children. It's recommended that you use the CtrlReactComponent class to take full advantage of the router.

String hash

The parsed hash.

Example:

// Route is /settings#name

class Test extends CtrlReactComponent {
  render() {
     const {hash} = this.context.router

     return (
       <div>
         <div>Hash: {hash}</div>
       </div>
     )
  }
}

<CtrlReactRouter>
  <Test/>
</CtrlReactRouter>

// Output:
// Hash: name

Object query

The parsed query object.

Example:

// Route is /login?type=user&id=1989

class Test extends CtrlReactComponent {
  render() {
    const {query} = this.context.router

    return (
      <div>
        <div>ID: {query.id}</div>
        <div>Type: {query.type}</div>
      </div>
    )
  }
}

<CtrlReactRouter>
  <Test/>
</CtrlReactRouter>

// Output:
// ID: 1989
// Type: user

Array route

The parsed route object.

Example:

// Route is /app/users/1989/profile

class Test extends CtrlReactComponent {
  render() {
    const {route} = this.context.router

    return (
      <ul>
        {route.map((part) => <li>part</li>)}
      </ul>
    )
  }
}

<CtrlReactRouter>
  <Test/>
</CtrlReactRouter>

// Output:
// - app
// - users
// - 1989
// - profile

Boolean filterChildren(Any child)

Decide whether or not a child component should be rendered depending on what their routeFilter method returns. If it does not have a routeFilter method, always render the child.

  • param Any child - Child to be filtered.
  • returns Boolean - Whether the child should be rendered or not.

Example:

// filterChildren is called internally by the router to determine which child
// components should be rendered.
<CtrlReactRouter>
  // If the route is /fruits, this component will be rendered and filterChildren
  // will be internally called to filter its child components.
  <CtrlReactComponent routeFilter={([category]) => category === 'fruits'}>
    // Since this component does not have a routeFilter method, it will always
    // be rendered as long as the route begins with /fruits.
    <h1>Fruits</h1>

    // If the route is /fruits/apple, this component will be rendered and
    // filterChildren will be internally called to filter its child components.
    <CtrlReactComponent routeFilter={([fruit]) => fruit === 'apple'}>
      
      // Since this component does not have a routeFilter method, it will always
      // be rendered as long as the route begins with /fruits/apple.
      <h2>Apple</h2>
    </CtrlReactComponent>
  </CtrlReactComponent>
</CtrlReactRouter>

Boolean updateRoute(String path, Number scroll)

Update the URL path. This also updates state and browser history.

  • param String path - Target relative URL path.
  • param Number scroll - Target scroll top.
  • returns Boolean - If the route was successfully updated.

Example:

class Link extends CtrlReactComponent {
  handleClick(event) {
    event.preventDefault()

    this.context.router.updateRoute(this.props.href)
  }

  render() {
    return (
      <a click={this.handleClick.bind(this)}/>
    )
  }
}

<CtrlReactRouter>
  <Link href='/target-path'/>
</CtrlReactRouter>

License

Copyright (c) 2017 Martin Experiments LLC

MIT (https://www.opensource.org/licenses/MIT)