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

@pure180/re-con

v1.1.5

Published

Basic React-Context application state management

Downloads

12

Readme

React Context Application State Management

This package provides a basic functionality to manage your react application state with the Facebook house made context API.

Working example: https://codesandbox.io/s/pure180-re-con-example-se718

I would appreciate any comment or suggestion, please let me know what you think.

Table of Contents

Installation

npm i @pure180/re-con

or

yarn add @pure180/re-con

Usage

Create a file for all configurations that controls the state handling.

e.g.: ./src/State.ts

import {
  AppState,
  BaseState,
  createAppStateContext,
  createAppStateProvider,
  createSelectorHook,
  Middleware,
} from '@pure180/re-con';

// Enum values which describes all available state properties
export enum StateKeys {
  Item = 'item',
}

// Enum values that describes the actions for on reducer
export enum EnumActions {
  Change = 'change',
}

// Describes one state object
export type StateObject = {
  item: string;
};

// Describes the hole state object including the reducer.
export interface State {
  [StateKeys.Item]: BaseState<StateObject, EnumActions>;
}

// Create the application Context which we will use later to create the
// provider or gaining access to the whole State-Context object of your
// application.
export const AppContext = createAppStateContext<
  State,
  State[keyof State]['state'],
  EnumActions
>();

// Create the application state provider that gives the react element
// children the ability to access the application state.
export const AppStateProvider = createAppStateProvider<
  State,
  State[keyof State]['state'],
  EnumActions
>(AppContext);

// Create the selector hook which is able to access one state object
// including the dispatch function.
export const useSelector = (key: keyof State) =>
  createSelectorHook(AppContext)(key);

// Create a default state including your reducer
export const defaultState: AppState<
  State,
  State[keyof State]['state'],
  EnumActions
> = {
  [StateKeys.Item]: {
    reducer: (state, action) => {
      switch (action.type) {
        case EnumActions.Change:
          return {
            ...state,
            ...action.payload,
          };
        default:
          return state;
      }
    },
    state: {
      item: 'myValue',
    },
  },
};

// Create a middleware that should run after the state mutation
const middlewareAfterMutation: Middleware<State, State[keyof State]['state'], EnumActions> =
  (state, action) => [Timing.After, () => {
    if (action.type === EnumActions.Change) {
      state[StateKeys.Item].state.item = 'change value after mutation'
    }
    return state;
  }
];

export const middleware: Middleware<State, State[keyof State]['state'], EnumActions>[] = [
  middlewareAfterMutation,
];

Apply the State Context Provider to your application.

e.g.: ./src/index.tsx

import React, { useCallback } from 'react';

import {
  AppStateProvider,
  defaultState,
  EnumActions,
  middleware,
  StateKeys,
  useSelector,
} from './State';

const ComponentWithSelectorHook: React.FunctionComponent = () => {
  const [state, dispatch] = useSelector(StateKeys.Item);

  const handleClick = useCallback(() => {
    const item = 'New Item Value';

    dispatch({
      type: EnumActions.Change,
      payload: { ...state, item },
    });
  }, [dispatch, state]);

  return (
    <div>
      <p>{state.item}</p>
      <button onClick={handleClick}>Click to change the State</button>
    </div>
  );
};

export const AppWithStateContextProvider: React.FunctionComponent = () => (
  <AppStateProvider state={defaultState} middleware={middleware}>
    <ComponentWithSelectorHook />
  </AppStateProvider>
);

export default AppWithStateContextProvider;

Functions

Typedefs

Timing : enum

Kind: global enum
Read only: true
Properties

| Name | Type | | --- | --- | | Timing.After | after | | Timing.Before | before |

createAppStateContext() ⇒ Context.<AppContextValue.<State, Item, ActionType>>

Kind: global function

useSelectorBase(Context, key) ⇒

Kind: global function
Returns: [AppState, (action: ActionReturn<ActionType, Item>) => void] - Returns a tuple of the selected state and the dispatch function

| Param | Type | Description | | --- | --- | --- | | Context | Context.<AppContextValue.<State, Item, ActionType>> | The state Context object | | key | string | The key of the state property |

createSelectorHook(Context) ⇒

Kind: global function
Returns: [AppState, (action: ActionReturn<ActionType, Item>) => void] - Returns a tuple of the selected state and the dispatch function

| Param | Type | Description | | --- | --- | --- | | Context | Context.<AppContextValue.<State, Item, ActionType>> | The state Context object |

resolveMiddleware(timing, state, action, middleware) ⇒ AppState

Kind: global function

| Param | Type | | --- | --- | | timing | Timing | | state | AppState | | action | ActionReturn | | middleware | Array.<Middleware> |

createAppStateProvider(Context) ⇒ React.FunctionComponent

Kind: global function

| Param | Type | Description | | --- | --- | --- | | Context | React.Context | Application state context |

Action ⇒ ActionReturn

Kind: global typedef

| Param | Type | Description | | --- | --- | --- | | type | ActionType | the string / action type the reducer is acting on | | payload | Payload | Object that mutates the State. |

ActionReturn : Object

Kind: global typedef
Properties

| Name | Type | Description | | --- | --- | --- | | type | ActionType | string | the string / action type the reducer is acting on | | payload | Payload | Object that mutates the state |

Actions : Object.<string, Action>

Kind: global typedef

BaseState : Object

Kind: global typedef
Properties

| Name | Type | Description | | --- | --- | --- | | reducer | Reducer | the reducer to mutate the state | | state | Object | Array | String | the state object |

AppState : Object.<string, BaseState>

Kind: global typedef

AppStateProviderProps : Object

Kind: global typedef
Properties

| Name | Type | Description | | --- | --- | --- | | log | boolean | Should log the application state to the console. | | middleware | Array.<Middleware> | undefined | Array of middleware functions that | | state | AppState | Object that holds the default state. |

AppContextValue : Object

Kind: global typedef
Properties

| Name | Type | Description | | --- | --- | --- | | dispatch | function | | | middleware | Array.<Middleware> | undefined | | | state | AppState | |

MiddlewareReturnFunction ⇒ AppState

Kind: global typedef

Middleware ⇒ Array.<Timing, MiddlewareReturnFunction>

Kind: global typedef

| Param | Type | Description | | --- | --- | --- | | state | AppState | the current state object | | action | ActionReturn | the current action |

Reducer : function

Kind: global typedef

| Param | Type | Description | | --- | --- | --- | | state | State | state that can be mutated by the reducer. | | action | ActionReturn | Object that should mutate the state. |

AppStateProvider ⇒ React.FunctionComponent

Kind: global typedef

| Param | Type | | --- | --- | | props | PropsWithChildren.<AppStateProviderProps> |