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

auto-react-context

v1.2.8

Published

auto context creation for react applications

Downloads

8

Readme

Auto React context


npm version

Lightweight and simple solution to react context creation.

Creating a React context is simple and straightforward, the tediousness begins when we need to deal with more than a single context.

Traditionally, you would need to go through the context creation boilerplate, each time you need to make a new one.

Solution

auto-react-context creates automatically a new context for you, all you have to do is follow the steps below.

Getting started

Installation

npm install auto-react-context

Let's use a simple book store application as an example.

The application let's you perform basic CRUD operations, to implement this feature using Context API with auto-react-context we would do the following :

// in your Context file
import createDataContext from "auto-react-context";

// book reducer function
const bookReducer = (state, action) => {
  switch (action.type) {
    case "ADD_BOOK":
      return { ...state, books: [...state.books, action.payload] };
    case "REMOVE_BOOK":
      return {
        ...state,
        books: state.books.filter((book) => book.id !== action.payload),
      };
    default:
      return state;
  }
};

// Create your action creators
const addBook = (dispatch) => (title, author) => {
  dispatch({ type: ADD_BOOK, payload: { title, author } });
};

const removeBook = (dispatch) => (id) => {
  dispatch({ type: REMOVE_BOOK, payload: id });
};

// here is where it all happens
// Export Context and Provider from createDataContext 
export const { Context, Provider } = createDataContext(
  // Pass the reducer as first argument
  bookReducer,
  // Pass an object with your action creators
  {
    addBook,
    removeBook,
  },
  //Pass your state object
  {}
);

Now that we exported the Context and The Provider, let's use them:

Provider


// In your App.js
// Rename Provider as it will collide with other providers, in case we have multiple instances
// in this use case bookProvider.
import { Provider as BookProvider } from "./context/BookContext";

function App() {
  return (
    <div className="App">
      <BookProvider>
       <OtherComponentsGoHere>
      </BookProvider>
    </div>
  );
}

Context


// Same here
import { Context as BookContext } from "../context/BookContext";

// here you have access to your BookContext
const BookList = () => {
  const {
    state: { books },
  } = useContext(BookContext);
  return books && books.length ? (
    <div className="book-list">
      <ul>
        {books.map((book) => {
          return <BookDetails book={book} key={book.id} />;
        })}
      </ul>
    </div>
  ) : (
    <div className="empty">No Books to read at this moment.</div>
  );
};