Socket
Socket
Sign inDemoInstall

react-context-connect

Package Overview
Dependencies
8
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-context-connect

A connector for the react context API


Version published
Weekly downloads
6
increased by100%
Maintainers
1
Install size
3.38 MB
Created
Weekly downloads
 

Readme

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

Last updated on 03 Apr 2018

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc