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

v2.0.0

Published

propmatch is a useful utility for all high order components. It's useful because strings are brittle identifiers in composition, and it allows you to see where props are coming from in the wrapped component.

Downloads

12

Readme

propmatch is a useful utility for all high order components. It's useful because strings are brittle identifiers in composition, and it allows you to see where props are coming from in the wrapped component.

Install

react-propmatch is available on npm.

npm install react-propmatch

Guide

Note: This guide assumes you're using es6 with babel, and the optional 'es7.classProperties' transform.

Let's start by writing a simple high order component that sends the current time as a prop. The details are omitted for brevity.

function providesTime(Component){

  return class TimeProvider extends React.Component {
    constructor(){ ... }
    componentDidMount(){ ... }
    componentWillUnmount(){ ... }

    render(){
      return <Component {...this.props} time={this.state.time} />
    }
  };
}

@providesTime
class Clock {
  render(){
    return <div>new Date(this.props.time).toString()</div>;
  }
}

Because string keys are brittle and conflict prone, and the origin of props is often unclear, we can use react-propmatch to enable a clearer way of describing the relationship.

import propMatch from 'react-propmatch';

var {propTypes, makeFactory} = propMatch({time: null});

function providesTime(Component){
  var {makeProps, restPropTypes} = makeFactory(Component);

  return class TimeProvider extends React.Component {
    static propTypes = restPropTypes;
    constructor(){ ... }
    componentDidMount(){ ... }
    componentWillUnmount(){ ... }

    render(){
      return <Component 
        {...this.props} 
        {...makeProps({time: this.state.time})}
      />
    }
  };
}

// expose the propTypes
providesTime.propTypes = propTypes;

@providesTime
class Clock {
  render(){
    return <div>new Date(this.props.time).toString()</div>;
  }
}

And this behaves exactly like the first example. So let's say that our clock want's to take a prop called 'time' which is a boolean. If true, don't render the date.

Damn, we have a conflict. Well instead of changing clock's api, or having to change the possibly third-party providesTime, we just declare our propTypes.

@providesTime
class Clock {
  static propTypes = {
    time: PropTypes.boolean,
    milliseconds: providesTime.propTypes.time,
  };

  render(){
    var d = new Date(this.props.milliseconds);
    return <div>{this.props.time ? d.toTimeString() : d.toString()}</div>;
  }
}

react-propmatch looks at the propTypes and tries to find a match. It falls back to using the keys you provide initially, which is why it defaults to 'time' here.

That's all, happy high order component making!