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 🙏

© 2024 – Pkg Stats / Ryan Hefner

redux-auto-actions

v1.0.2

Published

A React redux-thunk library to make your life esier

Downloads

127

Readme

REDUX-AUTO-ACTIONS

This module is aimed at reducing the termendous amount of boilerplate that plagues react-redux flow creations. It follows the Keep It Super Simple philosophy.

Import

import { StoreModule } from 'redux-auto-actions';

Usage

1 First create app-actions.ts

import { AnyAction } from 'redux';
import { ThunkAction } from 'redux-thunk';

import { GlobalState } from '../state';

import { ActionType, appS } from './app.state';

/**
 * Exportable Actions
 */
const increment = appS.setPayloadAction<number>(
  ActionType.INCREMENT,
  (state, action) => ({
    ...state,
    counter: state.counter + action.payload,
  })
);
const decrement = appS.setPayloadAction<number>(
  ActionType.DECREMENT,
  (state, action) => ({
    ...state,
    counter: state.counter - action.payload,
  })
);
const reset = appS.setSimpleAction(ActionType.RESET, () => appS.initialState);

/**
 * Thunks
 */
type AppThunks<R> = ThunkAction<R, GlobalState, null, AnyAction>;

type TestAyncThunk = (amount: number) => AppThunks<boolean>;
const testAsync: TestAyncThunk = (amount) => (dispatch) => {
  setTimeout(() => dispatch(increment(amount)), 1000);
  return true;
};

export const actions = {
  increment,
  decrement,
  reset,
  testAsync,
};

/**
 * Reducer
 */
export const AppReducer = appS.getReducer();

/**
 * Exportable Selectors
 */
export const selectors = appS.helper;

2 Now setup app.state.ts

import { StoreModule } from 'redux-auto-actions';

export interface AppState {
  counter: number;
}
export enum AppStateLabel {
  STATE = 'app',
}

export const appS = new StoreModule<ActionType, AppState>(AppStateLabel.STATE, {
  counter: 0,
});

export enum ActionType {
  INCREMENT = 'INCREMENT',
  DECREMENT = 'DECREMENT',
  RESET = 'RESET',
}

2 Now setup store store.ts

import { createStore, applyMiddleware, Store, Middleware } from 'redux';
import thunk from 'redux-thunk';

import { defaultState } from './state';
import { combinedReducers } from './reducer';

const middleware: Middleware[] = [thunk];

export const appStore: Store = createStore(
  combinedReducers,
  defaultState,
  applyMiddleware(...middleware)
);

3 Once you connect your store to the app, by means of setting up the Provider

import { Provider } from 'react-redux';

import { appStore } from './store/store';

// ...
<Provider store={store}>
  <App />
</Provider>
// ...

4 You can create your App.tsx

redux-auto-actions way

If you are using our cool Connect helper, which is aimed at removing TypeScript boilerplate around your component construction, you can think of any argument passed to stateAndDispatch() as the function signature of connect() from react-redux. Remember that for more advanced usages (using mergeParam or options) you will need to use react-redux boilerplate. More info in Redux.js docs - Typing the connect higher order component

import React from 'react';
import { Connect } from 'redux-auto-actions';

import { GlobalState } from './store';
import { selectors, actions } from './store/app.actions';
import { AppButton } from './components/Button';
import './App.css';

interface AppProps {}

export const Counter = Connect<GlobalState, AppProps>()
  .stateAndDispatch(
    /**
     * @implNote you can inject component props into mapStateToProps
     */
    (state, ownProps) => ({
      counter: selectors.counter(state),
    }),

    /**
     * @implNote you can inject component props into mapDispatchToProps
     * currently the <code>(dispatch, ownProps) => ({})</code> way is not available
     */
    {
      increment: actions.increment,
      decrement: actions.decrement,
      reset: actions.reset,
      testAsync: actions.testAsync,
    }
  )
  .withComp(({ counter, increment, decrement, reset, testAsync }) => (
    <div className="App">
      <header className="App-header">
        <h4>{counter}</h4>
        <br />
        <AppButton label="Increment" onClick={() => increment(1)} />
        <AppButton label="Decrement" onClick={() => decrement(1)} />
        <AppButton label="Reset" onClick={() => reset()} />
        <AppButton label="TestAsync" onClick={() => testAsync(10)} />
      </header>
    </div>
  ));

Traditional way

import React from 'react';
import { connect, ConnectedProps } from 'react-redux';

import { GlobalState } from './store';
import { selectors, actions } from './store/app.actions';
import { AppButton } from './components/Button';
import './App.css';

const connector = connect(
  (state: GlobalState) => ({
    counter: selectors.counter(state),
  }),
  {
    increment: actions.increment,
    decrement: actions.decrement,
    reset: actions.reset,
    testAsync: actions.testAsync,
  }
);
type PropsFromRedux = ConnectedProps<typeof connector>;

interface OwnArgs {}
interface AppProps extends PropsFromRedux, OwnArgs {}

const AppRaw: React.FC<AppProps> = ({
  counter,
  increment,
  decrement,
  reset,
  testAsync,
}) => (
  <div className="App">
    <header className="App-header">
      <h4>{counter}</h4>
      <br />
      <AppButton label="Increment" onClick={() => increment(1)} />
      <AppButton label="Decrement" onClick={() => decrement(1)} />
      <AppButton label="Reset" onClick={() => reset()} />
      <AppButton label="TestAsync" onClick={() => testAsync(10)} />
    </header>
  </div>
);
export const App = connector(AppRaw);

Have fun!

Help with work

Just fork and do a PR :) I will add you to the colaborators list with a BIG thank you!

  • If you want to buy me a coffee or a beer as a thank you, I'm very much appreciated :stuck_out_tongue_winking_eye: Donate

Guidelines

Whenever a new master is deployed, it should be tagged with the new deployed version. After we reach version 1.0.0 as the first release (production ready). After that, we follow semantic versioning.

Publishing

Remember to always publish on a merge request. Pipeline master:only actions will be created in the future, once we stabilize this library.

Enjoy!

Troubleshooting

  • Create an issue