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

react-redux-simple

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-redux-simple

A selector friendly shortcut for React-Redux

  • 0.6.0
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Redux Redux Simple

npm version Build Status

An alternative way to connect React components to a Redux store.

import React from "react";
import { connect } from "react-redux-simple";
import * as selectors from "./selectors";
import * as actions from "./actions";

class Counter extends React.Component {
  static selectors = {
    count: selectors.getCount
  }

  static actions = {
    inc: actions.increment,
    dec: actions.decrement
  }

  render() {
    let { count, inc, dec } = this.props;

    return (
      <div>
        <button onClick={dec}>-</button>
        <span>{count}</span>
        <button onClick={inc}>+</button>
      </div>
    );
  }
}

export default connect(Counter)

Or with a functional component:

const Counter = ({ count, inc, dec }) => (
  <div>
    <button onClick={dec}>-</button>
    <span>{count}</span>
    <button onClick={inc}></button>
  </div>
);

Counter.selectors = {
  count: selectors.getCount
};

Counter.actions = {
  inc: actions.increment,
  dec: actions.decrement
};

export default connect(Counter)

Getting Started

You'll need to install both this package and react-redux:

npm install react-redux react-redux-simple

Selectors

An optional mapping of prop names to selectors (a function which takes the state and returns a value).

static selectors = {
  count: state => state.count
}

If your selectors depend on the components own props, then selectors can be a function instead.

static selectors = props => ({
  count: state => state.count * props.multiplier
})

A common pattern is to name your selectors and group them in their own file (or colocate them with reducers).

import * as ToolSelectors from "./selectors/tool";
import * as ViewSelectors from "./selectors/view";

class Artboard extends Component {
  static selectors = {
    tool: ToolSelectors.getCurrentTool,
    width: ViewSelectors.getVisibleWidth,
    height: ViewSelectors.getVisibleHeight
  };
}

Actions

An optional mapping of prop names to action creators (a function which returns an action).

static actions = {
  increment: amount => ({ type: "INCREMENT", amount })
}

Another common pattern is to name and group actions in related files, then import them into the components that need them.

import * as ToolActions from "./actions/tool";
import * as ViewActions from "./actions/view";

class Artboard extends Component {
  static actions = {
    setTool: ToolActions.setCurrentTool,
    setWidth: ViewActions.setVisibleWidth,
    setHeight: ViewActions.setVisibleHeight
  };
}

What about <Provide />?

You still need to make sure that your components have access to the store. The easiest way to do this is to have a <Provide /> component at the root of your component tree.

import { Provide } from "react-redux"; // or from "react-redux-simple";

ReactDOM.render(
  <Provide store={myStore}>
    <App />
  </Provide>
);

What about mergeProps and options?

This is designed to be a stupidly simple wrapper, if you need more control you can just use the React-Redux version of connect for a component with specific needs.

FAQs

Package last updated on 07 Sep 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