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

context-spices

v0.1.8

Published

<img src='https://raw.githubusercontent.com/dhanielsales/context-spices/master/assets/context-spices.png' align='left' width='250px' height='250px' />

Downloads

8

Readme

context-spices

What is it about?

Just a few tools for working with React Context API in small projects.

Currently includes:

  1. createReducer - builds a structure for dispatch your Action Functions in each context.
  2. createActions - builds your Action Functions and Types for this Actions at the same time.

Installation

You just need node.js and Npm:

 npm install context-spices

or Yarn:

 yarn add context-spices

Demo - https://ducks-with-context.vercel.app/

Demo Repo - https://github.com/dhanielsales/ducks-with-context

Sample

Below are examples of each function individually.

createActions

Use the createActions function to build an object that contains as many actions as you need with their unique identifiers and their payloads.

enum TypesNames {
  LOGIN_SUCCESS = "LOGIN_SUCCESS",
}

interface LoginSuccessAction extends Action<TypesNames.LOGIN_SUCCESS> {
  email: string;
  token: string;
}

const actions = createActions<{
  loginSuccess: (email: string, token: string) => LoginSuccessAction;
}>(
  { loginSuccess: TypesNames.LOGIN_SUCCESS },
  { loginSuccess: ["email", "token"] }
);

const { loginSuccess } = actions;

console.log(
  loginSuccess(
    "[email protected]",
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMj..."
  )
);

// Outputs:
// {
//   type: 'LOGIN_SUCCESS',
//   payload: {
//     email: '[email protected]',
//     token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIi...',
//   },
// };

createReducer

Use the createReducer function to build a function to dispatch the actions and change a State and returns the results.

Normal Javascript Object

const INITIAL_STATE = {
  user: {
    email: "",
    token: "",
  },
};

type State = typeof INITIAL_STATE;

const loginSuccessReducer = (
  state: State,
  payload: LoginSuccessAction
): State => {
  return {
    ...state,
    user: {
      email: payload.email,
      token: payload.token,
    },
  };
};

const reducerTypes = {
  [TypesNames.LOGIN_SUCCESS]: loginSuccessReducer,
};

const mainReducer = (state: State, action: Action) =>
  createReducer<State>(state, action, reducerTypes);

console.log(
  mainReducer(
    INITIAL_STATE,
    loginSuccess(
      "[email protected]",
      "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMj..."
    )
  )
);

// Outputs:
// {
//   user: {
//     email: '[email protected]',
//     token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIi...',
//   },
// };

Immutable Objects

import produce from "immer";

const INITIAL_STATE = {
  user: {
    email: "",
    token: "",
  },
};

type State = typeof INITIAL_STATE;

const loginSuccessReducer = produce(
  (state: State, payload: LoginSuccessAction): State => {
    return {
      ...state,
      user: {
        email: payload.email,
        token: payload.token,
      },
    };
  }
);

const reducerTypes = {
  [TypesNames.LOGIN_SUCCESS]: loginSuccessReducer,
};

const mainReducer = (state: State, action: Action) =>
  createReducer<State>(state, action, reducerTypes);

console.log(
  mainReducer(
    INITIAL_STATE,
    loginSuccess(
      "[email protected]",
      "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMj..."
    )
  )
);

// Outputs:
// {
//   user: {
//     email: '[email protected]',
//     token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIi...',
//   },
// };

Fully Example

Below is a complete example of use with the React context API.

Context

First, create a folder in the project to save the files context, action and reducer files:

Second, within a context file, create a structure for context and useReducer as below:

import React, { createContext, useContext, useReducer } from "react";
import { Action } from "context-spices";
import { CounterReducer } from "./ducks";

export interface State {
  count: number;
}

interface ContextProps {
  state: State;
  dispatch?: React.Dispatch<Action>;
}

const INITIAL_STATE: State = {
  count: 0,
};

const CounterContext = createContext<ContextProps>({
  state: INITIAL_STATE,
});

export const ButtonsProvider: React.FC = ({ children }) => {
  const [state, dispatch] = useReducer(CounterReducer, INITIAL_STATE);
  return (
    <CounterContext.Provider value={{ state, dispatch }}>
      {children}
    </CounterContext.Provider>
  );
};

export const useCounters = () => useContext(CounterContext);

Third, create a file to save your actions and reducers that I will call the file ducks.ts.

Within this file, create a structure similar to this one, so that you can freely control its state:

import { Action, createActions, createReducer } from "context-spices";
import { State } from "./index"; // Context file

export enum TypesNames {
  INCREMENT = "INCREMENT",
  DECREMENT = "DECREMENT",
  SET_VALUE = "SET_VALUE",
}

export type Increment = Action<TypesNames.INCREMENT>;

export type Decrement = Action<TypesNames.DECREMENT>;

export interface SetValue extends Action<TypesNames.SET_VALUE> {
  value: number;
}

export const Creators = createActions<{
  increment: () => Increment;
  decrement: () => Decrement;
  setValue: (value: number) => SetValue;
}>(
  {
    increment: TypesNames.INCREMENT,
    decrement: TypesNames.DECREMENT,
    setValue: TypesNames.SET_VALUE,
  },
  {
    increment: null,
    decrement: null,
    setValue: ["value"],
  }
);

const setValueReducer = (state: State, { value }: SetValue): State => {
  return {
    ...state,
    count: value,
  };
};

const incrementReducer = (state: State): State => {
  return {
    ...state,
    count: state.count + 1,
  };
};

const decrementReducer = (state: State): State => {
  return {
    ...state,
    count: state.count - 1,
  };
};

const reducerTypes = {
  [TypesNames.INCREMENT]: incrementReducer,
  [TypesNames.DECREMENT]: decrementReducer,
  [TypesNames.SET_VALUE]: setValueReducer,
};

export const CounterReducer = (state: State, action: Action) =>
  createReducer<State>(state, action, reducerTypes);

References