npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@truefit/bach-redux

v2.1.0

Published

compose your react-redux components in style

Downloads

94

Readme

@truefit/bach-redux

This library brings redux connectivity to components composed with @truefit/bach allowing you to add actions and selectors directly in your enhancer chain, rather than needing an extra HOC for connect.

Installation

npm install @truefit/bach-redux redux react-redux

or

yarn add @truefit/bach-redux redux react-redux

Enhancers

withAction

Allows you to specify a single action creator to be connected to the store. See withActions for more discussion

Enhancer Signature

| Parameter | Type | Description | | ---------- | ---------------- | ------------------------------------------------------------------------------------------------- | | actionName | string | the name for the action creator in the props passed to the wrapped component | | fn | js function | the function that is executed when the action creator is invoked | | conditions | array of strings | names of the properties on the props object react should restrict the creation of the function to |

Example

Typescript

import React from 'react';
import {compose} from '@truefit/bach';
import {withAction} from '@truefit/bach-redux';
import {PayloadAction} from '@reduxjs/toolkit';

const ADD_TODO = 'ADD_TODO';
const addToDo = (note: string): PayloadAction<string> => ({
  type: ADD_TODO,
  payload: note,
});

type Props = {
  addToDo: (note: string) => void;
};

const WithAction = ({addToDo}: Props) => (
  <div>
    <h1>withAction</h1>
    <button
      type="button"
      onClick={() => {
        addToDo('New ToDo from withAction');
      }}
    >
      Click Me
    </button>
  </div>
);

export default compose(withAction<Props>('addToDo', addToDo))(WithAction);

Javascript

import React from 'react';
import {compose} from '@truefit/bach';
import {withAction} from '@truefit/bach-redux';

const ADD_TODO = 'ADD_TODO';
const addToDo = note => ({
  type: ADD_TODO,
  payload: note,
});

const WithAction = ({addToDo}) => (
  <div>
    <h1>withAction</h1>
    <button
      onClick={() => {
        addToDo('New ToDo from withAction');
      }}
    >
      Click Me
    </button>
  </div>
);

export default compose(
  withAction('addToDo', addToDo)
)(WithAction);

withActions

Allows you to specify a map of action creators to be connected to the store.

Side Note This functionality was removed from the react-redux standard hooks (https://react-redux.js.org/next/api/hooks#removed-useactions). That said, we don't agree with the reasoning. Although using dispatch directly in your components does match standard hooks more closely, that doesn't mean that it is better code. In our opinion, it leads to less readable code - thus we have included these enhancers in this library.

Enhancer Signature

| Parameter | Type | Description | | ---------- | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------- | | actions | js object | a js object that contains a map of keys and action creator functions. Each key will be a property passed to the wrapped component. | | conditions | array of strings | names of the properties on the props object react should restrict the creation of the functions to |

Example

Typescript

import React from 'react';
import {compose} from '@truefit/bach';
import {withActions} from '@truefit/bach-redux';
import {PayloadAction} from '@reduxjs/toolkit';

const ADD_TODO = 'ADD_TODO';

const addToDo1 = (note: string): PayloadAction<string> => ({
  type: ADD_TODO,
  payload: note,
});

const addToDo2 = (note: string): PayloadAction<string> => ({
  type: ADD_TODO,
  payload: `Too: ${note}`,
});

type Props = {
  addToDo1: (note: string) => void;
  addToDo2: (note: string) => void;
};

const WithActions = ({addToDo1, addToDo2}: Props) => (
  <div>
    <h1>withActions</h1>
    <button
      type="button"
      onClick={() => {
        addToDo1('New ToDo 1 from withActions');
      }}
    >
      Click Me
    </button>
    <button
      type="button"
      onClick={() => {
        addToDo2('New ToDo 2 from withActions');
      }}
    >
      Click Me
    </button>
  </div>
);

export default compose(
  withActions<Props>({addToDo1, addToDo2}),
)(WithActions);

Javascript

import React from 'react';
import {compose} from '@truefit/bach';
import {withActions} from '@truefit/bach-redux';

const ADD_TODO = 'ADD_TODO';

const addToDo1 = note => ({
  type: ADD_TODO,
  payload: note,
});

const addToDo2 = note => ({
  type: ADD_TODO,
  payload: `Too: ${note}`,
});

const WithActions = ({addToDo1, addToDo2}) => (
  <div>
    <h1>withActions</h1>
    <button
      onClick={() => {
        addToDo1('New ToDo 1 from withActions');
      }}
    >
      Click Me
    </button>
    <button
      onClick={() => {
        addToDo2('New ToDo 2 from withActions');
      }}
    >
      Click Me
    </button>
  </div>
);

export default compose(withActions({addToDo1, addToDo2}))(WithActions);

withDispatch

Returns a reference to the dispatch function from the Redux store. You may use it to dispatch actions as needed.

Enhancer Signature

There are no parameters for this enhancer.

Example

import React from 'react';
import {compose} from '@truefit/bach';
import {withDispatch} from '@truefit/bach-redux';
import {Dispatch} from 'redux';

const ADD_TODO = 'ADD_TODO';

type Props = {
  dispatch: Dispatch;
};

const WithDispatch = ({dispatch}: Props) => (
  <div>
    <h1>withDispatch</h1>
    <button
      type="button"
      onClick={() => {
        dispatch({
          type: ADD_TODO,
          payload: 'New ToDo from withDispatch',
        });
      }}
    >
      Click Me
    </button>
  </div>
);

export default compose(withDispatch())(WithDispatch);
import React from 'react';
import {compose} from '@truefit/bach';
import {withDispatch} from '@truefit/bach-redux';

const ADD_TODO = 'ADD_TODO';

const WithDispatch = ({dispatch}) => (
  <div>
    <h1>withDispatch</h1>
    <button
      onClick={() => {
        dispatch({
          type: ADD_TODO,
          payload: 'New ToDo from withDispatch',
        });
      }}
    >
      Click Me
    </button>
  </div>
);

export default compose(withDispatch())(WithDispatch);

React-Redux Hook

useDispatch

withSelector

Allows you to extract data from the Redux store state, using a selector function.

Enhancer Signature

| Parameter | Type | Description | | ------------ | ---------------- | ----------------------------------------------------------------------------------------------- | | selectorName | string | the name of the value in the props passed to the wrapped component | | fn | js function | the function that returns the desired value from the store | | conditions | array of strings | names of the properties on the props object react should restrict the firing of the function to |

Example

Typescript

import React from 'react';
import {compose} from '@truefit/bach';
import {withSelector} from '@truefit/bach-redux';
import {ApplicationState} from '../../../rootReducer';

const todoSelector = (state: ApplicationState) => state.features.example.test;

type Props = {
  todos: string[];
};

const WithSelector = ({todos}: Props) => (
  <div>
    <h1>withSelector</h1>
    <ul>
      {todos.map(todo => (
        <li key={todo}>{todo}</li>
      ))}
    </ul>
  </div>
);

export default compose(
  withSelector<Props, string[]>('todos', todoSelector)
)(WithSelector);

Javascript

import React from 'react';
import {compose} from '@truefit/bach';
import {withSelector} from '@truefit/bach-redux';

const WithSelector = ({todos}) => (
  <div>
    <h1>withSelector</h1>
    <ul>
      {todos.map(todo => (
        <li key={todo}>{todo}</li>
      ))}
    </ul>
  </div>
);

export default compose(
  withSelector('todos', (state, props) => state.features.bachRedux.todo),
)(WithSelector);

React-Redux Hook

useSelector

withStore

Returns a reference to the same Redux store that was passed in to the component. This enhancer should probably not be used frequently. Prefer withSelector() as your primary choice.

Enhancer Signature

There are no parameters for this enhancer.

Example

Typescript

import React from 'react';
import {compose} from '@truefit/bach';
import {withStore} from '@truefit/bach-redux';
import {Store} from 'redux';

type Props = {
  store: Store;
};

const WithStore = ({store}: Props) => (
  <div>
    <h1>withStore</h1>
    <ul>
      {store.getState().features.example.test.map((todo: string) => (
        <li key={todo}>{todo}</li>
      ))}
    </ul>
  </div>
);

export default compose(
  withStore()
)(WithStore);

Javascript

import React from 'react';
import {compose} from '@truefit/bach';
import {withStore} from '@truefit/bach-redux';

const WithStore = ({store}) => (
  <div>
    <h1>withStore</h1>
    <ul>
      {store.getState().features.bachRedux.todo.map(todo => (
        <li key={todo}>{todo}</li>
      ))}
    </ul>
  </div>
);

export default compose(
  withStore(),
)(WithStore);

React-Redux Hook

useStore