Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-context-connect

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-context-connect

A connector for the react context API

  • 1.1.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

react-context-connect

A connector for the react context API

Install

npm install react-context-connect

Usage

import React from "react";
import { render } from "react-dom";
import connectContext from 'react-context-connect';

const resourceContext = React.createContext(); // creating a context

const Greetings = ({ resource, name }) => (
  <div>{`${resource.Hello} ${name} :)`}</div>
);

const mapContextToProps = data => ({
  resource: data
});

const ConnectedGreetings = connectContext(
  resourceContext.Consumer,
  mapContextToProps
)(Greetings);

const BlackBox = props => {
  return (
    <div>
      <h3>I'm A Black Box</h3>
      <ConnectedGreetings name="John" />
    </div>
  );
};

class App extends React.Component {
  state = {
    lang: "en",
    resources: {
      en: {
        Hello: "Hello"
      },
      sp: {
        Hello: "Hola"
      }
    }
  };
  setLang = ({ target }) => this.setState({ lang: target.value });
  render() {
    const { resources, lang } = this.state;
    return (
      <div>
        <select onChange={this.setLang}>
          <option value="en">English</option>
          <option value="sp">Spanish</option>
        </select>
        <resourceContext.Provider value={resources[lang]}>
          <BlackBox />
        </resourceContext.Provider>
      </div>
    );
  }
}

render(<App />, document.getElementById("root"));

Live Demo

PlayGround

Keywords

FAQs

Package last updated on 03 Apr 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