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

create-react-context-data

v1.0.3

Published

Easily create Context and Provider in your React application without repeating boilerplate code.

Readme

Create React Context Data


npm version

Simple approach to avoid repeating boilerplate for React Context and Provider.

Every time we need create a new Context (and its Provider) inside a project, we repeat the same boilerplate logics to handle its data and the actions related to it.

create-react-context-data provides a handy function to immediately create Context and Provider for you.

All you need to do is define your reducer function and the actions you need to handle your data. Under the hood the package uses the useReducer hook and bind the dispatch function to all the actions.

Installation

npm install create-react-context-data

Basic Usage

Every time you need to create a new Context, you can create a file and use the createReactDataContext function provided by the package.

You need to provide a reducer, the actions and the initial state. The function will return your Context and Provide which can be exported from the file and imported wherever you need them.

//import the createReactDataContext function inside your new Context file
import createReactDataContext from 'create-react-context-data';

// import or define your reducer, actions and initial state

export const { Context, Provider } = createReactDataContext(
  reducer,
  actions,
  initialState
);

Full Example

First of all, you need to create a file for the Context you want to create. For this example, we'll use ItemContext.js

// #1 import the createReactDataContext function
import createReactDataContext from 'create-react-context-data';

// #2 define your reducer function
const itemReducer = (state, action) => {
  const { type, payload } = action;
  switch (type) {
    case 'CREATE_ITEM':
      return { ...state, items: [...state.items, payload] };
    case 'DELETE_ITEM':
      return {
        ...state,
        items: state.items.filter((item) => item.id !== payload),
      };
    default:
      return state;
  }
};

// #3 define your actions

// dispatch is provided by default by create-react-context-data
const createItem = (dispatch) => (newItem) => {
  dispatch({ type: 'CREATE_ITEM', payload: newItem });
};

const deleteItem = (dispatch) => (id) => {
  dispatch({ type: 'DELETE_ITEM', payload: id });
};

const myActions = { createItem, deleteItem };

const initialState: { items: [] };

//#3 call the  createReactDataContext function
const { Context, Provider } = createReactDataContext(
  itemReducer,
  myActions,
  initialState
);

//#4 export them to be available elsewhere
export { Context, Provider };

Once the Context and The Provider have been exported, they can be used as follows:

Provider In App.js we want to use the Provider to wrap the other components


// Import and rename the provider with a suitable name to prevent collision with other providers
import { Provider as ItemProvider } from './context/ItemContext';

function App() {
  return (
    <div className="App">
      <ItemProvider>{/* ... all your other components */}</ItemProvider>
    </div>
  );
}

Context


import React, { useContext } from 'react';
// Import and rename the context with a suitable name to prevent collision with other contexts
import { Context as ItemContext } from '../context/ItemContext';

// You can now access the Context as you would normally do
const ToDoList = () => {
  const { state } = useContext(ItemContext);
  const { items } = state;
  if (!items || !items.length) return null;
  return (
    <ul>
      {items.map((item) => (
        <li key={item.id}>{item.text}</li>
      ))}
    </ul>
  );
};