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 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-enhanced-context

v0.3.2

Published

use react Hooks and Context api to manage state, thanks to @react-simply/state

Readme

Welcome to React-Enhanced-Context!

Use react Hooks and Context to enhance react context for managing state. Thanks for @react-simply/state that be heavily borrowed by react-enhanced-context.

Install


npm i react-enhanced-context

yarn add react-enhanced-context

Example (default usage)


import  React  from  "react"

import { useFriends } from  "./states"

import { EnhancedContextProvider, EnhancedContextConsumer, useEnhancedContext } from  'react-enhanced-context'


export  default  function  MyFriendsOnlineContextView() {

	//use provider to set initialState and reducers
	return  <EnhancedContextProvider  initialState={{ online: { isOnline:  false }, friends: [] }}  reducers={{
		friends: (state, action) => {
			if (action.type === 'new-friend') {
				return [...action.friends]
			}
			return  state;
		}
	}}  >
		<MyFriendsOnlineView  />
	</EnhancedContextProvider>
}

function  FriendListItem(props) {

//pick sub state from state tree
const [online, setState] =  useEnhancedContext('online');
const  isOnline  =  online.isOnline;
return (
		<li  style={{ color:  isOnline ? 'green' : 'black' }}>
			{props.friend.name}
			<button  onClick={() => {
			
				//merge current state to previous state
				setState({ isOnline:  true })
			}}></button>
			{props.children}
		</li>
	);
}

  

function  MyFriendsOnlineView() {
	const  myFriends  =  useFriends();

	//get state tree and default dispatch method
	const [{ friends }, dispatch] =  useEnhancedContext();
return  <div>
			<ul>
				{myFriends.length === 0 ? <div>loading my friends...</div> : myFriends.map(friend  =>
				<FriendListItem  key={friend.id}  friend={friend}  ><FriendStatus  friend={friend}  /></FriendListItem>)}
			</ul>
			<button  onClick={() => {
			
				//dispatch state to reducers
				dispatch({ type:  'new-friend', friends: [{ name:  `new1${Date.now()}` }] })
			}}></button>
			<div  style={{ display:  friends.length > 0 }}>{friends.length > 0 ? friends[0].name : ''}</div>
		</div>
}

  

function  FriendStatus(props) {
	
	// use Context.Consumer not Hooks
	return  <EnhancedContextConsumer>{([context]) => {
		const  online = context.online;
		return  ` => ${online.isOnline ? 'Online' : 'Offline'}`;
	}}</EnhancedContextConsumer>
}

Example (multi-context)

import  createEnhancedContext  from  'react-enhanced-context'

//could create multi-context by createEnhancedContext
const {
EnhancedContext,
EnhancedContextProvider,
EnhancedContextConsumer,
useEnhancedContext
} =  createEnhancedContext();

Options

createEnhancedContext

There is a default context had been created, no need to use the method, however if you want to use multi contexts, you could create much.

defaultValue (optional)

default value is an object, and it will pass to React.createContext, not recommended to use.

calculateChangedBits (optional)

default value is undefined, and it will pass to React.createContext, not recommended to use.

EnhancedContextProvider

initialState (optional)

should be an object that be overrided the defaultValue from createEnhancedContext.

reducers (optional)

should be an object that has propName from initialState. If dont pass reducers, only rootReudcer could be used.

{
	friends: (state, action) => {
		if (action.type === 'new-friend') {
			return [...action.friends]
		}
		return  state;
	}
}

useEnhancedContext

key (optional)

If key being passed, the default reducer will be used, and the method returns sub state, otherwise the reducers passed to the Provider will be used, and the method returns state tree.

return

the return value would be an array, the first will be sub state or state tree, and the second will be a method about merging to sub state or dispatch to reducers.