Skip to content

A component wrapper for the connect method from react-redux, Using the renderProp pattern (PROOF OF CONCEPT)

License

Notifications You must be signed in to change notification settings

juliankrispel/redux-connector

Repository files navigation

DISCLAIMER: This component is a proof of concept. Use it with care. It'll work but has one performance downside:

  • If any of the props of this Connector component update, it'll rerender its entire subtree.

Redux Connector

Install from npm - yarn add redux-connector or npm install redux --save

A component alternative for the connect HOC from react-redux, using the Function as a child or render prop pattern.

<Connect
  mapStateToProps={mapStateToProps}
  mapDispatchToProps={mapDispatchToProps}
>
  {({ currentUser, login }) => (
    { currentUser.isLoggedIn
    ? <HomeScreen />
    : <LoginScreen login={login} />}
  )}
</Connect>

Rationale

Higher order components are used to wrap other components. This component enables you to use connect straightforwardly within jsx, removing much of the cognitive burden of using connect and refactoring components to use connect.

API:

Redux Connector mirrors the api from the connect method of react-redux. To configure the component, simply add the arguments as props.

Props

  • mapStateToProps - (Function),
  • mapDispatchToProps - (Function)
  • mergeProps - (Function)
  • options - (Object)

A more complete Example

import React from 'react';
import Connector from 'redux-connector';
import { loginAction } from '../actions/user';

const mapStateToProps = ({ currentUser }) => ({ currentUser });
const mapDispatchToProps = { login: loginAction };

const Root = () => (
  <Connect
    mapStateToProps={mapStateToProps}
    mapDispatchToProps={mapDispatchToProps}
  >
    {({ currentUser, login }) => (
      { currentUser.isLoggedIn
      ? <HomeScreen />
      : <LoginScreen login={login} />}
    )}
  </Connect>
)

Shoutout to Max Stoiber for putting this into my head!

About

A component wrapper for the connect method from react-redux, Using the renderProp pattern (PROOF OF CONCEPT)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published