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-resource-detector

v1.0.0

Published

A resource detector for React components

Downloads

9

Readme

react-resource-detector

Build Status

A resource detector for React, based on react-onroutechanged.

The react-resource-detector is a React Higer Order Component(HOC) that you can use with your own React components if you want to detect the resources in route when route changes.

Installation

Use npm

npm install react-resource-detector

Use yarn

yarn add react-resource-detector

Usage

ES6 Class Component

import React from 'react'
import routeResourceDetectorHOC from 'react-resource-detector'

class StudentInfo extends React.PureComponent {
  this.resourceConfigurations = {
    'class/:classId': {
      // if detect resources in sequence, handler should return a promise.
      handler: (matches, path, location) => {
        // ...detect resource
        /*
         * matches: { classId: value }
         * path: /class/class-id
         * location: Current location object
         */
      }
    },
    'student/:studentId': {
      handler: (matches, path, location) => {}
    }
  }

  this.routeConfigurations = {
    '/class/:classId/student/:studentId': {
      handler: (matches, path, location) => {},
      deselect: (matches, path, prevLocation) => {},
      exact: true,
      shouldDetectResource: true,
      whiteList: ['student/:studentId'],
      blackList: ['class/:classId']
    },
  }

  render () {
    return (
      <div>student</div>
    )
  }
}

export default routeResourceDetectorHOC(StudentInfo, {
  shouldDetectResourceForAllRoutes: false,
  detectResourceInSequence: true,
  deselectResources: true
})

Functional Component

import React from 'react'
import routeResourceDetectorHOC from 'react-resource-detector'

const StudentInfo = (props) => {
  StudentInfo.resourceConfigurations = {} // The same as ES6 Class Component
  StudentInfo.routeConfigurations = {}    // The same as ES6 Class Component
}

export default routeResourceDetectorHOC(StudentInfo)

routeResourceDetectorHOC

  • First parameter: Decorated Component.
  • Second parameter: Global detection configuration object.

Global Detection Configuration

| Name | Type | Default | Description | | -------------- | --------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | shouldDetectResourceForAllRoutes | boolean | true | If true, the resources lies in all routes will be detected by default. | | detectResourceInSequence | boolean | false | If true, the resources will be detected in sequence, it will stop detecting resources when error occurs. See more in example. | | deselectResources | boolean | false | If true, resource detector will invoke a deselect function provided by current route right before the route changes. See more in example. |

Resource Configuration

  • The resourceConfigurations is a dictionary of resource detection configurations. The key is a resource pattern, and the value is a resource detection configuration for this resource.
  • resourceConfigurations must be provided, otherwise an error will be reported.

Resource Pattern

A resource pattern can be either a string or a regexp object.

Resource Detection Configuration

| Name | Type | Default | Description | | -------------- | --------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | handler | function | | (matches, path, location) => {}, this function will be triggered when there is a match between the resource pattern and the route. matches is the map of param name and value lies in the pattern, path is the exact match result of the pattern, location is the current location object. |

Route Configuration

  • The routeConfigurations is a dictionary of route processing configurations. The key is a route pattern, and the value is a route processing configuration for this route.
  • routeConfigurations is optional, if not provided, all of the routes will be checked against the resource configurations to find out if there are some matches.

Route Pattern

A route pattern can be either a string or a regexp object.

Route Processing Configuration

| Name | Type | Default | Description | | -------------- | --------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | handler | function | | Same as the resource configuration above. | | deselect | function | | (matches, path, prevLocation) => {}, if deselectResources is true, this function will be triggered when there is a matched route of previous location in routeConfigurations before route changes. matches is the map of param name and value lies in the pattern, path is the exact match result of the pattern, prevLocation is the previous location object. | | exact | boolean | true | If true, the handler function will be triggered when the route completely matches the route pattern. | | shouldDetectResource | boolean | true | If true, the resources lies in the route will be detected. | | whiteList | array | | Array of resource patterns. Resources in the whiteList will be detected. | | blackList | array | | Array of resource patterns. Resources in the blackList will not be detected. |

WhiteList and BlackList

  • If only whiteList is provided, the route will only be checked against the whitelisted resource patterns.
  • If only blackList is provided, the route will be checked against the resource patterns of resourceConfigurations except the blacklisted ones.
  • If both whiteList and blackList are provided, the route will be checked against the whitelisted resource patterns that are not in blackList.