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

ezcontext

v1.0.2

Published

A implementation of ACV (Action → Context → View) for React

Downloads

9

Readme

ezcontext

A implementation of ACV (Action → Context → View) for React

Features

  1. Lightweight
  2. Based on new React context API
  3. Support using multiple contexts at once
  4. Support computed contexts
  5. Support High Order Component creator

ezcontext in actions

  1. Basic Functions https://codesandbox.io/s/m14qn9kyy
  2. Todo App https://codesandbox.io/s/y3qlo47vxz

Samples

create(value:any, methods):Context

Create a new Context with specified value and method list. If no methods specified, ezcontext creates default wired method which named update(newState)

import { create } from "ezcontext";

const UserContext = create("guest", {
  loginAsAdmin: () => "admin",
  logout: () => "guest"
});

console.log(UserContext()); // => "guest"
UserContext.loginAsAdmin();
console.log(UserContext()); // => "admin"
UserContext.logout();
console.log(UserContext()); // => "guest"

create(value:any, methodCreator:(contextAccessor)=>Object):Context

Create a new Context with specified value and methodCreator. methodCreator retrieves contextAccessor and must return method list

import { create } from "ezcontext";

const Todos = create([], context => ({
  // load todos from server
  load() {
    // perform lazy update todos once data is loaded
    return fetch(`http://tempuri.org/todos`)
      .then(res => res.json())
      .then(todos => context(todos));
  }
}));

create(...contexts:Context[]):ComputedContext

Create a new ComputedContext which has one or many dependency Context. Once dependency contexts updated, ComputedContext will be recomputed

import { create } from "ezcontext";

const ContextA = create(1);
const ContextB = create(2);
const ContextAB = create(
  ContextA,
  ContextB,
  (aValue, bValue) => aValue + bValue
);

console.log(ContextAB()); // => 3

Create Provider

import { create, use } from "ezcontext";
const ContextA = create(1);
const ContextB = create(2);
const Provider = use(ContextA, ContextB);

const App = () => <Provider>App content</Provider>;

Create wired component

import { create, use } from "ezcontext";
const IdsContext = create([1, 2]);
const TodosContext = create({
  1: { text: "item 1" },
  2: { text: "item 2" }
});

const TodoList = use(
  IdsContext,
  TodosContext,
  ($ids, $todos) => ({ ids: $ids.value, todos: $todos.value }),
  props => {
    /* render todo list */
  }
);

Create an action connected with multiple contexts

import { create, use } from "ezcontext";
const IdsContext = create([1, 2]);
const TodosContext = create({});

const AddTodo = use(IdsContext, TodosContext)(text => ($ids, $todos) => {
  const id = new Date().getTime();
  $ids.add(id);
  $todos.add(id, { text });
});

AddTodo("New Todo");

Create High Order Component

import React from "react";
import { create, use } from "ezcontext";

const UserContext = create("guest");
const Authenticate = use(UserContext)(
  allowUser => $user => (Component, props) =>
    allowUser === $user.value ? (
      <Component {...props} />
    ) : (
      <div>Access denied</div>
    )
);
const AdminScreen = Authenticate("admin")(props => {
  /* render admin screen here */
});

Create HOC from context

import React from "react";
import { create, use } from "ezcontext";

const UserContext = create("guest");
const UserHoc = UserContext.hoc("username");
const UserProfile = UserHoc(props => <div>{props.username}</div>);

Create HOC from multiple contexts

import React from "react";
import { create, use } from "ezcontext";

const UserContext = create("guest");
const ThemeContext = create("default");
const DataContext = create("data");
const Hoc = use(
  UserContext.hoc("user"),
  ThemeContext.hoc("theme"),
  DataContext.hoc("data")
);
const Dashboard = Hoc(props => {
  /* render dashboard */
});