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

contextism

v1.3.2

Published

New way to use React Context

Downloads

11

Readme

Contextism 🤩 is a new way to use React Context better.

Read this article to become familiar with the idea. Downloads Size License

Installation 🔥

npm i contextism
// or 
yarn add contextism

Usage ✏️

We have two ways to use Contextism, Creating store using it or using its hooks directly:

#1 createStore ✋

// store.js 
import { createStore } from 'contextism';
const context = createStore("default value for state");
export const { Provider, useDispatchContext, useStateContext, useStore } = context;

// App.jsx
import Div from './Div'
import { Provider } from './store';

const App = () => {
	const [ state, dispatch ] = React.useState("Value for state"); // or useReducer
	
	return (
		<Provider state={state} dispatch={dispatch}>
			// Components you want to use the state there.
			<Div />
		</Provider>
		)
}

// Div.jsx
import { useStateContext, useDispatchContext, useStore } from './store';

const Div = () => {
	const state = useStateContext(); // "Value for state"
	const dispatch = useDispatchContext(); // dispatch function (setState) in App
	// or better one
	const [state, dispatch] = useStore();
	
	return ...
}

When we create store using Contextism, it gives us 3 hooks :

  • useStateContext: the state value that we gave it to state prop in Provider component

  • useDispatchContext: the setState function or useReducer dispatch that we passed it to dispatch prop

  • useStore: returns us an array with two values of the above hooks; [ useStateContext, useDispatchContext ]

    NOTE: you should use these hooks( methods of createStore function) in child components of Provider component.

#2 default hooks ✋

Contextism has two hooks beside createStore function:

  • useContext: takes a React context and returns the value
  • useStore: takes two React contexts and returns two values of them, the same thing like in the above way but with two arguments
// Store.jsx
export const CountStateContext = React.createContext();
export const CountDispatchContext = React.createContext();
function countReducer(state, action) {
  ...
}

export function CountProvider({children}) {
  const [state, dispatch] = React.useReducer(countReducer, {count: 0});
  return (
    <CountStateContext.Provider value={state}>
      <CountDispatchContext.Provider value={dispatch}>
        {children}
      </CountDispatchContext.Provider>
    </CountStateContext.Provider>
  )
}
// App.jsx
import { CountProvider } from './Store';
import Div from './Div';
export function App() {
	return (
		<CountProvider>
		<Div />
		</CountProvider>
	)

}
// Div.jsx
import { CountStateContext, CountDispatchContext } from './Store';
import { useContext, useStore} from 'contextism';

export function Div() {
	const state = useContext(CountStateContext);
	const dispatch = useContext(CountDispatchContext);
	// Or much better:
	const [state, dispatch] = useStore(CountStateContext,CountDispatchContext);
	
	return ...

}

Typescript 🔷

Contextism has Typescript support like generics and ... . in createStore you can pass two generics too, first one for the state structure and interface, the second one for the useReducer hook.

type Action = {type: 'increment'} | {type: 'decrement'}
type State = { count: number }
// The second generic is for useReducer Action
const context = createStore<State, Action>();

// For useState just pass the first generic (State interface generic)
const context = createStore<State>();

Contribution

I'm developer, not a perfect person, so I make much mistakes, it means that be free to create issues and then PRs.

Thanks ❤️

Special thanks for contributing and using this project.