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

provide-router

v2.0.0

Published

Provides routing to React components.

Downloads

22

Readme

provide-router

build status npm version npm downloads

Provides routing to React components.

If you're using react-router v2 or v3, you'll want v1 of this library.

Table of contents

  1. Installation
  2. Usage
  3. Actions
  4. Reducers
  5. Example

Installation

npm install provide-router --save

Usage

This is a provider factory (see react-redux-provide) that creates a router provider using your desired history implementation.

// client

import provideRouter from 'provide-router';
import { createBrowserHistory } from 'history';

const history = createBrowserHistory();
const router = provideRouter(history);
// server

import provideRouter from 'provide-router';
import { createMemoryHistory } from 'react-router';

const history = createMemoryHistory({ initialEntries: [ request.url ] });
const router = provideRouter(history);

Actions

pushRoute (Object location | String path)

Essentially history.push(location).

replaceRoute (Object location | String path)

Essentially history.replace(location).

goBack (Number n = 1)

Essentially history.go(-1 * n);

goForward (Number n = 1) {

Essentially history.go(n);

Reducers

location

The current location via history. react-router's Switch and Route components rely on this. Your own components can use this too, of course.

history

The history object, synchronized with the provider's store. This will be automatically passed to the Router component. You probably won't ever use this but it's there if you need it.

Example

// src/components/App.js

import React from 'react';
import { Router, Switch, Route } from 'react-router';
import { Awesome, Home, Foo, Bar } from './index';

// `history` is automatically provided to `Router`
// and `location` is automatically provided to `Switch` and `Route`
const App = () => (
  <Router>
    <Awesome>
      <Switch>
        <Route exact path="/" component={Home} />
        <Route path="/foo" component={Foo} />
        <Route path="/bar" component={Bar} />
      </Switch>
    </Awesome>
  </Router>
);

export default App;
// src/renderApp.js (usually client-side)

import 'react-redux-provide/lib/install';
import React from 'react';
import { render } from 'react-dom';
import App from './components/App';
import defaultProps from './defaultProps';
import provideRouter from 'provide-router';
import createBrowserHistory from 'history/createBrowserHistory';

const history = createBrowserHistory();

defaultProps.providers.router = provideRouter(history);

function renderApp(props, element = document.getElementById('root')) {
  return render(<App { ...props }>, element);
}

renderApp(defaultProps);

export default renderApp;
// src/renderAppToString.js (usually server-side)

import 'react-redux-provide/lib/install';
import React from 'react';
import { renderToString } from 'react-dom/server';
import App from './components/App';
import defaultProps from './defaultProps';
import provideRouter from 'provide-router';
import createMemoryHistory from 'history/createMemoryHistory';

// You should actually set this per request within the props passed to
// the `renderAppToString` function, but just for the sake of the example...
const history = createMemoryHistory({ initialEntries: [ request.url ] });

defaultProps.providers.router = provideRouter(history);

function renderAppToString(props = defaultProps) {
  return renderToString(<App { ...props } />);
}

export default renderAppToString;