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

component-connector

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

component-connector

Easily and cleanly connect your components to many higher order components

  • 1.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

react-component-connector

Easily and cleanly connect your components to many higher order components. This package was created to provide a cleaner way to connect a component to many higher order components (HoC). For example consider a component connected to a redux store

The Problem

import connect from 'react-redux';

class MyConnectedComponent extends React.Component {
    static propTypes: {
        storeValue1: PropTypes.number
    }
    ...
}

export default connect(
    (state) => ({
        storeValue1: state.value1
    }), 
    (dispatch) => ({
        ...
    }))(MyConnectedComponent);

This by itself isn't too bad, but consider if you wanted to pass this store connected component to another higher order component that extended its functionality.

...

class MyConnectedComponent extends React.Component {
    static propTypes: {
        ....
        myExtendedFunctionality: PropTypes.func
    }
    ....
}


export default extendFunctionality({
    doAwesomeStuff: true
}, connect(
   (state) => ({
       storeValue1: state.value1
   }), 
   (dispatch) => ({
       ...
   }))(MyConnectedComponent));

notice we keep the pattern of taking config and returning a function that takes a component with only two connections this is already getting pretty hard to read.

The solution

...
export default connectComponent(MyConnectedComponent)
    .toStore(
       (state) => ({
           storeValue1: state.value1
       }), 
       (dispatch) => ({
           ...
       }))
    .toExtendedFunctionality({
        doAwesomeStuff: true
    });

Use a function that is pre-configured with the connectors that your app uses, and provides a declarative api for connecting. toStore and toExtendedFunctionality are names for connectors. We keep the pattern of connectors which are functions that take configuration and return a function that takes a component (and probably wraps the component in an HoC)

Usage

You first need to configure you connectComponent function with the connectors your app uses

/* connectComponent.js */
import componentConnector from 'react-component-connector';
import { connect } from 'react-redux';
import extendedConnector from './extendedConnector';

// Ordering of connectors DOES matter
const myConnectors = [
    {
        name: 'toStore',
        connector: connect
    },
    {
        name: 'toExtendedFunctionality',
        connector: extendedConnector
    },
    ...
];

export default componentConnector(myConnectors);

connectComponent will apply connectors in order they show up in your connectors list this is important because some HoC may expect some piece of data to be on props that another connector supplied

Keywords

FAQs

Package last updated on 27 Jun 2017

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