Socket
Socket
Sign inDemoInstall

create-react-provider

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

create-react-provider

Creating context provider with ease, can be used as HOC or renderChildren


Version published
Weekly downloads
3
increased by200%
Maintainers
1
Weekly downloads
 
Created
Source

Create react provider

npm versionBuild Status

Introdution

Providers are a powerful tools in react, we all have used them by popular libraries such as redux, mobx, material-ui.

The thing is, providers can be created even for a smaller purpose than data management and UI library, you can create ModalManager, translations injections, NotificationManager and so much more!

This library creates a Provider for you to use, and a Hoc and a RenderChildrenComponent for you to use in your sub-components.

Usage

  • Install
      npm install --save create-react-provider
      # or yarn
      yarn add create-react-provider
    
  • import and use!
    import createReactProvider from 'create-react-provider';
    import PropTypes from 'prop-types';
    
    const {
      Provider: ThemeProvider,
      Hoc: ThemeHoc,
      RenderChildren: ThemeRenderChildren,
    } = createReactProvider({
      primaryColor: PropTypes.string,
      secondaryColor: PropTypes.string,
    });
    
    // ****************
    // Use provider in top layer of your react app
    // ****************
    class App extends Component {
      render () {
        return (
          <ThemeProvider>
            <SomeComponent />
          </ThemeProvider>
        );
      }
    }
    
    // ****************
    // Consume provided props in either way (Hoc or renderChildren)
    // ****************
    
    // renderChildren way
    class SomeComponentUsingRenderChildren extends Component {
      render() {
        return (
          <ThemeRenderChildren
            render={({ primaryColor: backgroundColor, secondaryColor: titleColor }) => (
              <div style={{ backgroundColor }}>
                <h1 style={{ color: titleColor }}>
                  I'm a title!
                </h1>
              </div>
            )}
          />
        );
      }
    }
    
    // Higher order component way
    @ThemeHoc(ctx => ({
      titleColor: ctx.primaryColor,
      backgroundColor: ctx.secondaryColor,
    }))
    class SomeComponentUsingHoc extends Component {
      render() {
        const { titleColor, backgroundColor } = this.props;
    
        return (
          <div style={{ backgroundColor }}>
            <h1 style={{ color: titleColor }}>
              I'm a title!
            </h1>
          </div>
        );
      }
    }
    
    // You can override Hoc props
    <SomeComponentUsingHoc /> // Will use titleColor from provider
    <SomeComponentUsingHoc titleColor="red" /> // Will use from given prop
    

Keywords

FAQs

Package last updated on 06 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc