All Projects → Spyna → React Context Hook

Spyna / React Context Hook

Licence: mit
A React.js global state manager with Hooks

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Context Hook

useStateMachine
The <1 kb state machine hook for React
Stars: ✭ 2,231 (+4362%)
Mutual labels:  hooks, state-management, state-machine
Radioactive State
☢ Make Your React App Truly Reactive!
Stars: ✭ 273 (+446%)
Mutual labels:  hooks, state-management
use-tiny-state-machine
A tiny (~700 bytes) react hook to help you write finite state machines
Stars: ✭ 37 (-26%)
Mutual labels:  hooks, state-machine
Redux Machine
A tiny library (12 lines) for creating state machines in Redux apps
Stars: ✭ 338 (+576%)
Mutual labels:  state-machine, state-management
statebot
Write more robust and understandable programs. Statebot hopes to make Finite State Machines a little more accessible.
Stars: ✭ 24 (-52%)
Mutual labels:  state-management, state-machine
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (+8%)
Mutual labels:  hooks, state-management
Beedle
A tiny library inspired by Redux & Vuex to help you manage state in your JavaScript apps
Stars: ✭ 329 (+558%)
Mutual labels:  state-machine, state-management
xstate-cpp-generator
C++ State Machine generator for Xstate
Stars: ✭ 33 (-34%)
Mutual labels:  state-management, state-machine
Machinery
State machine thin layer for structs (+ GUI for Phoenix apps)
Stars: ✭ 367 (+634%)
Mutual labels:  state-machine, state-management
Easy Peasy
Vegetarian friendly state for React
Stars: ✭ 4,525 (+8950%)
Mutual labels:  hooks, state-management
React Hooks
Essential set of React Hooks for convenient Web API consumption and state management.
Stars: ✭ 515 (+930%)
Mutual labels:  hooks, state-management
zoov
Use 🐻 Zustand with Module-like api
Stars: ✭ 24 (-52%)
Mutual labels:  hooks, state-management
micro-observables
A simple Observable library that can be used for easy state management in React applications.
Stars: ✭ 78 (+56%)
Mutual labels:  hooks, state-management
zedux
⚡ A high-level, declarative, composable form of Redux https://bowheart.github.io/zedux/
Stars: ✭ 43 (-14%)
Mutual labels:  state-management, state-machine
react-smart-app
Preconfiguration React + Ant Design + State Management
Stars: ✭ 13 (-74%)
Mutual labels:  hooks, state-management
Constate
React Context + State
Stars: ✭ 3,519 (+6938%)
Mutual labels:  hooks, state-management
Pullstate
Simple state stores using immer and React hooks - re-use parts of your state by pulling it anywhere you like!
Stars: ✭ 683 (+1266%)
Mutual labels:  hooks, state-management
xstate-viz
Visualizer for XState machines
Stars: ✭ 274 (+448%)
Mutual labels:  state-management, state-machine
Pvm
Build workflows, activities, BPMN like processes, or state machines with PVM.
Stars: ✭ 348 (+596%)
Mutual labels:  state-machine, state-management
Little State Machine
📠 React custom hook for persist state management
Stars: ✭ 654 (+1208%)
Mutual labels:  state-machine, state-management

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:

  • withStore - docs
  • useStore - docs
  • useStoreState - docs
  • useSetStoreValue - docs
  • useDeleteStoreValue - docs
  • GetAndSet - docs
  • GetAndDelete - docs
  • SetAndDelete - docs
  • StoreValue - docs

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].