![Maven Central Adds Sigstore Signature Validation](https://cdn.sanity.io/images/cgdhsj6q/production/7da3bc8a946cfb5df15d7fcf49767faedc72b483-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
@dteel/use-stored-reducer
Advanced tools
useStoredReducer, kind of like useReducer except it will persist in localStorage. Will stay in sync with other hooks across your app and across brower windows.
useStoredReducer is a reducer hook that syncs to localStorage. If the localStorage value is changed, useStoredReducer updates to the new value. If another useStoredReducer hook in the application is utilizing the same keyName, the update is almost immediate.
For performance optimization, it optionally takes a hysterisis argument, which is how long it should wait before comitting the new value to localStorage (the change is made immediately to all the current app's hooks, but it will wait to write to localStorage). This prevents writing to the disk until the state has not changed in XXX milliseconds. If not specified, the localStorage is written to immediately. Writing immediately should be fine for simple values, but if using JSON.stringify/JSON.parse with large objects, it might make more sense to set the hysterisis to 100ms.
Install the package from npm with
npm install @dteel/use-stored-reducer
And import it into one of your react components
import {useStoredReducer} from '@dteel/use-stored-reducer';
The hooks signature is
const [state, {current: dispatchRef}] = useStoredReducer(keyName, reducer, initialValue, storageObject, hysterisis=null)
The parameters to your reducer function are,
Whatever you return from your reducer function is what the state gets set to.
Below is a simple example
function reducer(state, action, payload, callback) {
switch (action) {
case 'age':
return {...state, age: Number(payload)};
case 'name':
return {...state, name: payload};
case 'reset':
return {age: 0, name: ''};
case 'get':
callback(state);
return state;
default:
throw Error('undefined action');
}
}
import { useStoredReducer } from '@dteel/use-stored-reducer';
//The reducer function we pass to the hook, when you call dispatchRef.current(action, payload)
//this function gets called with the current state and the action/payload you passed to it.
//what you return is what the state gets set to
function reducer(state, action, payload) {
switch (action) {
case 'age':
return {...state, age: Number(payload)};
case 'name':
return {...state, name: payload};
default:
return state;
}
}
export default function ExampleComponent(){
//We're reading/writing to localStorage key 'person' with a hysterisis of 500ms
//If someones typing, it wont save to storage until they have a 500ms break in key events
const [state, {current: dispatch}] = useStoredReducer('person', reducer, {age: 0, name: ''}, localStorage, 500);
return (
<>
<input type='text' value={state?.age || ' '} onChange={ (e) => dispatch('age', e.target.value) } />
<input type='text' value={state?.name || ' '} onChange={ (e) => dispatch('name', e.target.value) } />
</>
);
}
FAQs
useStoredReducer, kind of like useReducer except it will persist in localStorage. Will stay in sync with other hooks across your app and across brower windows.
The npm package @dteel/use-stored-reducer receives a total of 0 weekly downloads. As such, @dteel/use-stored-reducer popularity was classified as not popular.
We found that @dteel/use-stored-reducer 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
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.