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.
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>
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.
Redux Connector mirrors the api from the connect
method of react-redux. To configure the component, simply add the arguments as props.
mapStateToProps
- (Function),mapDispatchToProps
- (Function)mergeProps
- (Function)options
- (Object)
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!