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

v1.2.2

Published

A React component that takes a path, fetches data, and passes the data to its child component.

Downloads

16

Readme

react-controller npm package

react-controller is a component for React that takes a path, fetches a JSON response, and passes the object to its child component. Page caching, custom loading screens, prefetching and server-side rendering are made incredibly easy, especially when paired with react-router.

Installation

$ npm i react-controller --save
import Controller from 'react-controller'

The UMD build is also available on unpkg:

<script src="https://unpkg.com/react-controller/umd/react-controller.min.js"></script>

When using the UMD build, you can find the library on window.ReactController.

Basic Usage

This simplified example will make an XHR request to /api/product/foo-bar and pass the resulting JSON object to the Child component. Until the request is returned, the ProductLoading component will be rendered.

<Controller path="/api/product/foo-bar" loadingComponent={ProductLoading}>
  <Product />
</Controller>

Props

  • path string (Required) The endpoint to request data from.

  • children function or ReactElement (Required) Can be either a typical React element or a function.

  • cache boolean (Default: false) Setting to true will store all previously requested path data in the Controller state.

  • initialProps object (Default: null) If the app is server rendered, pass the in initialProps to avoid an extra XHR request when the Controller component mounts.

  • loadingComponent function (Default: null) The React component that should render while the requested path is loading.

  • onError function A function that will be called if the requested path returns anything other than a 200 header.

  • forceReload boolean (Default: false) If cache is turned on, but a certain path should always make an XHR request to the server, set forceReload to true.

  • isStatic boolean (Default: false) If the child component doesn’t need to make an XHR request to the server to render, then set isStatic to true.

  • options object (Default: {}) Object containing parameters for the request. (See the fetch spec.)

Handling Errors

There are two different ways to handle errors. The first is to pass a function into the onError prop of the Controller component. If no error occurs, then the object returned from your path will be spread across your child component's props.

import React from 'react'
import Controller from 'react-controller'

const ErrorHandler = ({ status }) => (
  <div>{status} status header.</div>
)

const App = () => (
  <Controller path="/api/product/foo-bar" onError={ErrorHandler}>
    <Product />
  </Controller>
)

The second, option is to pass a function as the children prop of Controller. This allows you to choose to pass only specific parts of the returned object.

import React from 'react'
import Controller from 'react-controller'

const ErrorHandler = ({ status }) => (
  <div>{status} status header.</div>
)

const App = () => (
  <Controller path="/api/product/foo-bar">
    {(error, props) => error ? (
      <ErrorHandler status={error.status} />
    ) : (
      <Product {...props} />
    )}
  </Controller>
)

Prefetching

If you would like to prefetch a page, use the this.context.controller.prefetch() function.

import React, { Component } from 'react'
import PropTypes from 'prop-types'

class Homepage extends Component {
  static contextTypes = {
    controller: PropTypes.object.isRequired
  }

  componentDidMount() {   
    this.context.controller.prefetch('/products')
  }

  ...
}

Example

To run the example application, run these commands from inside the /example directory.

$ npm i
$ npm start

View the example at http://localhost:3000.