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-app-controller

v0.5.1

Published

React application controller

Downloads

4

Readme

react-app-controller

DEPRECATION WARNING: This is not maintained anymore, please use react-router-component instead.

Application controller component for React.

It keeps track of window.location (via History API) and renders UI according to its routes table. It can be used both on client and server.

Installation

Install via npm:

% npm install react-app-controller

You certainly will need to install React itself:

% npm install react

Creating a controller

You can use react-app-controller to control how components are rendered in browser according to window.location:

var React = require('react');
var createController = require('react-app-controller');

var MainPage = React.createClass({
  ...
});

var AboutPage = React.createClass({
  ...
});

var controller = createController({
  routes: {
    '/': MainPage,
    '/about': AboutPage
  }
});

Instantiated controller is essentially a React component (one you would usually create with React.createClass(...) function).

Client side usage

When we are ready to start our controller in a browser we use its .render() static method instead of React.renderComponent.

controller.render(document.body, function(err, controller) {
  // controller instantiated and rendered into DOM
});

Now controller is fully functional, it listens to popstate event and react accordingly.

Transitions to different routes

Method .navigate(url) can be used to navigate to a specified URL:

controller.navigate('/about');

Another method .navigateQuery(obj) can be used to update just the current query string values:

controller.navigateQuery({search: 'term'});

Both these methods call window.pushState(..) internally so browser location will be updated accordingly.

You probably would want to use these methods when some event occurs like clicking an anchor element.

Server side usage

The same controller can be used to pre-generate UI markup on a server:

var createController = require('react-app-controller');

var controller = createController({
  routes: {
    '/': MainPage,
    '/about': AboutPage
  }
});

Method .renderToString(url, cb) takes a URL and produces corresponding markup asynchronously:

controller.renderToString('/about', function(err, markup) {
  // serve markup to a client
});

Handling NotFoundError

When no route is matched for a specified URL you can define renderNotFound() method to generate UI for this case:

var controller = createController({
  routes: {
    ...
  },

  renderNotFound: function() {
    return (
      <div className="NotFound">
        Sorry, no item could be found for a specified request.
      </div>
    );
  }
});

If no renderNotFound() was defined and condition occurs then NotFoundError will be thrown.

Overriding .render() method

Controllers are React components but they have .render() method implemented by default. It looks like this:

render: function() {
  return React.DOM.div(null, this.state.page);
}

Note the this.state.page, it is the currently active component according to window.location and routing table (routes attribute you passed as a part of a controller specification in createController).

In case there were no matches for a current URL then this.state.page will be null. You should handle this case according your needs.

You can override the .render() by own implementation, just pass it as a part of controller specification into createController.