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

monorouter-react

v0.4.2

Published

React tools for monorouter

Downloads

4

Readme

monorouter-react

ReactJS utils for monorouter.

Usage

var monorouter = require('monorouter');
var reactRouting = require('monorouter-react');

monorouter()
  .setup(reactRouting()) // Use all the utilities
  .route('myurl', function(req) { // Route as normal
    // snip
  });

Or you can use the utilities individually (as shown below).

Utilities

reactEngine

The React engine is used to tell the router how to render React stuff:

var monorouter = require('monorouter');
var reactEngine = require('monorouter-react/lib/engine');

monorouter({engine: reactEngine}) // Use the React engine for rendering.
  .route('myurl', function(req) {
    this.render(function() {
      return <div>Hello world!</div>;
    });
  });

domCache

The DOM cache middleware is used to cache data in (and read data from) the DOM. This is very important for isomorphic React apps because the initial state of the browser app much match what was sent by the server. If you're getting your data from a service, it's possible that your server and client could get different results. To address this, the DOM cache plugin serializes data into the DOM and (on the browser side), read its from the DOM. This guarantees the data is the same on both sides of the wire.

The middleware adds a domCache method to the routing context (this in your route handlers) which takes three arguments:

  • key: A cache key to use for the data
  • getter(cb): A function used to look up the data when it can't be found in the cache. The function is passed a callback with the signature (err, data) which it should invoke once the data is loaded (or if there is an error).
  • callback(err, data): A function which will be invoked after the data is available.

This middleware will also pass a prop to your view named domCache which you use to place the cache in your template.

var monorouter = require('monorouter');
var domCache = require('monorouter-react/lib/domCache');

monorouter({engine: reactEngine})
  .use(domCache())
  .route('myurl', function(req, next) {
    this.domCache('mycachekey', function(cb) {
      // This function is for loading your data. It's only called if the data
      // can't be found in the cache. After it's loaded, pass it to the
      // callback. Since the data will be serialized into the response, it's
      // best to only pass what you need.
      xhr('http://example.com/people.json', function(err, data) {
        if (err) return cb(err);
        cb(null, {name: data.people[0].name});
      });
    }, function(err, data) {
      // This function is called after the data is ready.
      if (err) return next(err);
      this.render(function(props) {
        // Render a view using our loaded data. Don't forget to place the cache
        // too!
        return (
          <div>
            <h2>The first person is {data.name}!</h2>
            {props.domCache()}
          </div>
        );
      });
    });
  });