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 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-ctx

v0.2.1

Published

Higher order component wrappers for providing context

Downloads

20

Readme

react-ctx

npm version

Higher order component for interacting with context

npm install --save react-ctx

Why?

You're making a library and need to provide context to the component hierarchy. You don't want to use the context API directly, so you go with the Provider/connect pattern from redux. Then you realize you want to add more things to context, and... what if other libraries put something in context under the same key? You're bouncing back and forth between files for handling contextTypes and childContextTypes, which really aren't the purpose of your library. You don't want to worry about this, so you look for a simple wrapper and find react-ctx.

Simple Usage

import React, { Component } from 'react'
import { MapPropsToContext, MapContextToProps } from 'react-ctx'

const Text = ({foo}) => <div>{foo}</div>

export default class App extends Component {
  render() {
    return (
      // foo is added to props
      <MapPropsToContext foo={'bar'}>
        {/* foo is pulled from context and passed to Text as a prop */}
        <MapContextToProps foo>
          <Text />
        </MapContextToProps>
      </MapPropsToContext>
    )
  }
}

Provider / connect-style Usage

import React, { Component } from 'react'
import { MapPropsToContext, context } from 'react-ctx'

const contextTypes = {
  foo: PropTypes.string,
}

const MyProvider = ({foo}) => <MapPropsToContext foo={foo} contextTypes={contextTypes} />
const myConnect = () => context(contextTypes)

PropTypes

You can use PropTypes for validation.

import React, { Component, PropTypes } from 'react'
import { MapPropsToContext, MapContextToProps } from 'react-ctx'

const Text = ({foo}) => <div>{foo}</div>

const contextTypes = {
  foo: PropTypes.string,
}

export default class App extends Component {
  render() {
    return (
      // foo is added to props and its type is checked
      <MapPropsToContext
        foo={'bar'}
        contextTypes={contextTypes}
      >
        {/* all props described by contextTypes are grabbed from context and put into props */}
        <MapContextToProps contextTypes={contextTypes}>
          <Text />
        </MapContextToProps>
      </MapPropsToContext>
    )
  }
}

Namespaces

import React, { Component } from 'react'
import { MapPropsToContext, MapContextToProps } from 'react-ctx'

const Text = ({foo}) => <div>{foo}</div>

export default class App extends Component {
  render() {
    return (
      // yolo.foo is added to context
      <MapPropsToContext
        foo={'bar'}
        contextNamespace={'yolo'}
      >
        {/* yolo.foo is added to props as foo */}
        <MapContextToProps
          foo
          contextNamespace={'yolo'}
        >
          <Text />
        </MapContextToProps>
      </MapPropsToContext>
    )
  }
}

Wrapping Custom Components

import React, { Component } from 'react'
import { context, setContext } from 'react-ctx'

const contextTypes = {
  foo: PropTypes.string,
}

@setContext(contextTypes, 'namespace')
class FooProvider extends Component {
  render() {
    return (
      <div>
        {this.props.children}
      </div>
    )
  }
}

@context(contextTypes, 'namespace')
class Text extends Component {
  render() {
    return <div>{this.props.foo}</div>
  }
}

export default class App extends Component {
  render() {
    return (
      // namespace.foo is added to context
      <FooProvider foo={'bar'}>
        {/* namespace.foo is added to props as foo */}
        <Text />
      </FooProvider>
    )
  }
}