
Research
Malicious fezbox npm Package Steals Browser Passwords from Cookies via Innovative QR Code Steganographic Technique
A malicious package uses a QR code as steganography in an innovative technique.
groundstate
Advanced tools
[!WARNING] It's
@t8/react-store
now.
Minimalist shared state management for React apps
useState()
Installation: npm i groundstate
Moving the local state to the full-fledged shared state:
import {createContext, useContext} from 'react';
+ import {Store, useStore} from 'groundstate';
+
+ let AppContext = createContext(new Store(0));
let Counter = () => {
- let [counter, setCounter] = useState(0);
+ let [counter, setCounter] = useStore(useContext(AppContext));
let handleClick = () => {
setCounter(value => value + 1);
};
return <button onClick={handleClick}>{counter}</button>;
};
let ResetButton = () => {
- let [, setCounter] = useState(0);
+ let [, setCounter] = useStore(useContext(AppContext), false);
let handleClick = () => {
setCounter(0);
};
return <button onClick={handleClick}>×</button>;
};
let App = () => <><Counter/>{' '}<ResetButton/></>;
🔹 The Groundstate's shared state setup is very similar to useState()
allowing for quick migration from local state to shared state or the other way around.
🔹 The false
parameter in useStore(store, false)
(as in <ResetButton>
above) tells the hook not to subscribe the component to tracking the store state updates. The common use case is when the component doesn't make use of the store state value, but it may use the state setter.
🔹 An application can have as many stores as needed, whether on a single React Context or multiple Contexts.
let AppContext = createContext({
users: new Store(/* ... */),
items: new Store(/* ... */),
});
🔹 Apart from a boolean, useStore(store, shouldUpdate)
can take a function of (nextState, prevState) => boolean
as the second parameter to filter store updates to respond to:
let ItemCard = ({id}) => {
let hasRelevantUpdates = useCallback((nextItems, prevItems) => {
return nextItems[id].revision !== prevItems[id].revision;
}, [id]);
let [items, setItems] = useStore(
useContext(AppContext).items,
hasRelevantUpdates,
);
return (
// content
);
};
🔹 Shared state can be provided to the app by means of a regular React Context provider:
let App = () => (
- <AppContext.Provider value={42}>
+ <AppContext.Provider value={new Store(42)}>
<PlusButton/>{' '}<Display/>
</AppContext.Provider>
);
🔹 A store can contain data of any type.
Live demos:
Primitive value state
Object value state
🔹 Immer can be used with useStore()
just the same way as with useState()
to facilitate deeply nested data changes.
🔹 A store initialized outside a component can be used as the component's remount-persistent state.
FAQs
Minimalist shared state management for React apps
The npm package groundstate receives a total of 7 weekly downloads. As such, groundstate popularity was classified as not popular.
We found that groundstate demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Research
A malicious package uses a QR code as steganography in an innovative technique.
Research
/Security News
Socket identified 80 fake candidates targeting engineering roles, including suspected North Korean operators, exposing the new reality of hiring as a security function.
Application Security
/Research
/Security News
Socket detected multiple compromised CrowdStrike npm packages, continuing the "Shai-Hulud" supply chain attack that has now impacted nearly 500 packages.