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

react-context-hook

v3.0.7

Published

A React.js global state manager with Hooks and Context API

Downloads

1,651

Readme

react-context-hook

A React global state manager with Hooks

NPM Bundlephobia

Install

npm install --save react-context-hook

Documentation

Read the documentation of react-context-hook:

Usage

First, wrap your React App in the Store Provider using the function withStore.

import { withStore} from 'react-context-hook'

export default withStore(App)

Next use the hook in your Components

import { useStore } from 'react-context-hook'

// const [count, setCount, deleteCount] = useStore('count', 0)

export default function () {
  const [count, setCount, deleteCount] = useStore('count', 0)
  return (
    <>
      <button onClick={() => setCount(count - 1)}>Decrement - </button>
      <span className="count">{count}</span>
      <button onClick={() => setCount(count + 1)}>Increment + </button>
      <button onClick={() => deleteCount()}>Delete "count" from store</button>
    </>
  )
}

More hooks:

Advanced use of withStore

import { withStore} from 'react-context-hook'

const initialState = { count: 10 }

const storeConfig = {
  listener: (state, key, prevValue, nextValue) => {
      console.log(`the key "${key}" changed in the store`)
      console.log('the old value is', prevValue)
      console.log('the current value is', nextValue)
      console.log('the state is', state)
  },
  logging: true //process.env.NODE_ENV !== 'production'
}

export default withStore(App, initialState, storeConfig)

withStore creates a React Context.Provider which wrap your application the value of the Provider is the store. The store is similar to a Javascript Map where you can put, read, and delete values by their key.

For example store.set('username', {name: 'spyna', email: '[email protected]'}) will set a username key in the store map. Then you can read, modify or delete it from the map using store.get('username') or store.remove('username').

The store is exported as store from react-context-hook, so you can use it in the Components. However for convenience, in React Components you can use the hooks exported from react-context-hook.

Using store outside of React components

react-context-hook exports a store object which can be used outside of React Components, but will affect the global state. This object has the following methods:

  • reset: a function to reset the store state to the specified value: Eg: store.reset({initialValue: 0}).
  • set: a function that sets the specified key in the store. This function is equivaluent to the useSetStoreValue hook.
  • delete: a function that deletes the specified key from the store. This function is equivaluent to the useDeleteStoreValue hook.
  • getState: a function that returns the global state value of the store

when using these functions, the React Components will re-render.

Examples

import {store} from 'react-context-hook' //import the raw store


//In any JavaScript file you can use:

store.set('user', {name: 'piero', email: '[email protected]'})
store.delete('user')
store.reset({user: null, loggedIn: false})
const theState = store.getState()

This store is automatically initialized when you use the withStore function (export default withStore(App)). This means you can use it only after calling that function.

License

MIT © Spyna

Analytics