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

v1.0.2

Published

HOC for react network hooks

Downloads

2

Readme

react-hooker

this is a simple HOC for organizing network behaviour

import React, { Component } from 'react';
import hooker from 'react-hooker';

class App extends Component {
  static hooks = {
    loadItems: ()=> fetch('/items')
                       .then( response => response.json() )
                       .then( items => ({ items }) ),

    loadFakeItems: ()=> Promise.resolve({
      items: [
        { id: 0, displayName: 'cat', price: 3.5 },
        { id: 1, displayName: 'phone', price: 35 },
      ]
    }),

    loadSyncItems: ()=> ({
      items: [
        { id: 0, displayName: 'cat', price: 3.5 },
        { id: 1, displayName: 'phone', price: 35 },
      ]
    })
  }

  componentDidMount(){
    // use this when there is network connectivity
    this.props.loadItems();

    // use this one when in offline mode!
    // this.props.loadFakeItems();

    // or
    // this.props.loadSyncItems();
  }

  render(){
    const { items=[] } = this.props;

    return (
      <ul>
        {items.map( item => (
          <li key={item.id}>
            {item.displayName} = ${item.price}
          </li>
        ) )}
      </ul>
    );
  }
}

export default hooker(App);

for observables / sockets / triggering updates from a / calling a hook multiple times

class ticker {
  constructor( next, done, err ){
    this.next = next, this.done = done, this.err = err;
    this.count = 0;
  }

  cleanup(){
    clearInterval(this.interval);
  }

  handleRequest(...args){
    if(this.interval) clearInterval(this.interval);

    if( !args[0] ) this.done();
    else if( args[0] < 0 ) this.err('negative tick length not allowed');
    else this.interval = setInterval(()=> this.next({ count: this.count++ }), args[0]);
  }
}

export { ticker }; // from ./network, into the static hooks eventually

this example will make an interval of whatever length you tell it when calling the hook, to update props.count

this.props.ticker(1000);

calling it again will clear the old interval so you can change the tick length

this.props.ticker(2000);

without restarting the count

cleanup will be called when the component dismounts (componentWillUnmount)

or when calling the handler with 0

this.props.ticker(0);

by triggering the done callback

...

errors end up on this.props[hookName+'HookErr']

this.props.ticker(-1);

//...

render(){
  console.log(this.props.tickerHookErr); // 'negative tick length not allowed'
  return (<JSX/>);
}

you can also return a promise from handleRequest that will be the result of calling the hook each time, resolved after the setState is done (and so your props will be updated)