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

react-redux-methods

v1.1.4

Published

A lightweight react-redux toolkit for writing strong-typed, minimal code redux boilerplate.

Downloads

24

Readme

React-Redux Methods

A lightweight react-redux toolkit for writing strong-typed, minimal code redux boilerplate.

Installation

npm install react-redux-methods

How to use?

Creating Redux Contexts (reducer, actions, selectors)

import { createReduxMethods, ComposePayload } from 'react-redux-methods';

interface INotification {
  isOpen: boolean;
  message: string;
}

const initialState: INotification = {
  isOpen: false,
  message: '',
};

const [notificationsReducer, notificationsActions, notificationsSelectors] = createReduxMethods({
  initialState,
  reducers: {
    setNotificationShow: (state, action) => ({
      ...state,
      isOpen: true,
      message: action.payload,
    }),
    setNotificationHide: () => initialState,
    // You can define your own type for payload using 'ComposePayload' generics. In this case, we use 'string' type
    // for a 'message' property. This provides typescript intellisense for your payload when dispatching an action.
    updateNotification: (s, a: ComposePayload<string>) => ({
      ...s,
      message: a.payload,
    }),
  },
  selectionNode: 'globals.notifications', // <--- in this case, 'notifications' is located in 'globals' node of the
  // redux state tree. 'selectionNode' parameter should always be defined when 'selectors' parameter is defined.
  selectors: {
    getNotification: (s) => s,
    getNotificationMessage: (s) => s.message, // < -- under the hood, the actual output of your selector is
    // '(state) => state.globals.notifications.message' but you only provide a shorthand version with typescript
    // intellisense.

    // Please take note that you can apply 'reselect' api here since it is one of the dependencies of this package
    // (see 'reselect' documentation).
  },
});

// export all
export { notificationsReducer, notificationsActions, notificationsSelectors };

You would then combine your reducers, actions, and selectors.

import { combineReducers } from 'redux';

import { notificationsReducer, notificationsActions, notificationsSelectors } from './notifications';

// combine reducers
const globalReducer = combineReducers({
  notifications: notificationsReducer,
  // ...other reducers
});

// combine actions
const actions = {
  ...notificationsActions,
  // ...other actions
};

// combine actions
const selectors = {
  ...notificationsSelectors,
  // ...other selectors
};

Consuming redux contexts (for your react component)

Instead of using these common 'connect' or 'useSelector'/'useDispatch' methods from 'redux'/'react-redux', you can connect redux to your component using our helper functions. So that it would utilize typescript intellisense and all of your 'actions' and 'selectors' would easily be accessed by your component.

You must first define your redux connector ONCE only. Then you can consume it by all of your react components that utilize redux state.

In this case we will place our connector functions in the 'connections.ts' file.

import { createConnector, createDispatch, createGroupDispatch, createStateSelector } from 'react-redux-methods';

import store from './store';
import selectors from './selectors';
import actions from './actions';

export const reduxConnector = createConnector(selectors, actions);
export const dispatchAction = createDispatch(store.dispatch, actions);
export const makeGroupDispatch = createGroupDispatch(actions);
export const getValue = createStateSelector(store.getState(), selectors);

You would then consume the created connections to your component.

import type { ConnectedProps } from 'react-redux';
import { reduxConnector } from './connections';

// 'reduxConnector' function provides typescript intellisense for react-redux's 'connect' api.
// This means that all of your pre-defined 'selectors' and 'actions' will be provided by typescript
// to your component.
const connector = reduxConnector(
  (selectors) => ({ notification: selectors.getNotification }),
  (actions) => ({ setNotificationShow: actions.setNotificationShow }),
);

type Props = ConnectedProps<typeof connector>;

const App = ({
  notification, // <-- type inference is 'INotification' as defined above.
  setNotificationShow, // <-- type inference is '(payload: string) => INotification'
}: Props) => {
  return (
    <div>
      <p>`Notification: ${notification.message}`</p>
      <button onClick={() => setNotificationShow('Hello World!')}>Show Notification</button>;
    </div>
  );
};

export default connector(App);

You can also dispatch an action or get a state outside of your react component.

import { dispatchAction, getValue } from './connections';

const toastNotificationShow = (message) => {
  // ....your code logic

  // all of the parameters are also powered by typescript
  // the first parameter is the 'action' to dispatch and the other is the 'payload'
  dispatchAction('setNotificationShow', message);
};

const getNotification = () => {
  // ...your code logic

  // the parameter is the 'selector' name which is also powered by typescript.
  return getValue('getNotification');
};