![PyPI Now Supports iOS and Android Wheels for Mobile Python Development](https://cdn.sanity.io/images/cgdhsj6q/production/96416c872705517a6a65ad9646ce3e7caef623a0-1024x1024.webp?w=400&fit=max&auto=format)
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
react-context-hook
Advanced tools
A React global state manager with Hooks
npm install --save react-context-hook
Read the documentation of react-context-hook
:
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>
</>
)
}
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: 'me@spyna.it'})
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.
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:
store.reset({initialValue: 0})
.useSetStoreValue
hook.useDeleteStoreValue
hook.when using these functions, the React Components will re-render.
import {store} from 'react-context-hook' //import the raw store
//In any JavaScript file you can use:
store.set('user', {name: 'piero', email: 'nappiero@spyna.it'})
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.
MIT © Spyna
FAQs
A React.js global state manager with Hooks and Context API
We found that react-context-hook demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.