Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
zustand-di
Advanced tools
dependency injection for zustand with react context. initialize zustand stores with props.
npm install react zustand zustand-di
Note:
zustand-di
requiresreact
andzustand
as peer dependencies.
For
zustand@4.1.0
and higher, you needzustand-di@0.0.7
or higher.
For
zustand@~4.0.0
, you needzustand-di@0.0.6
or lower.
import { create } from "zustand";
import { createContext } from "zustand-di";
// 0. (TypeScript Only) Define a store
type CounterState = {
count: number;
inc: () => void;
};
// 1. Create the context
const [Provider, useStore] = createContext<CounterState>();
// 2. Create the store
const createStore = (initialState: { count: number }) =>
create<CounterState>((set) => ({
count: initialState.count,
inc: () => set((state) => ({ count: state.count + 1 })),
}));
// 3. Use the store in a child component
function Counter() {
const { count, inc } = useStore((state) => state);
return (
<>
<button onClick={inc}>increment</button>
<div>count: {count}</div>
<>
);
}
// 4. Use the store in the app and pass in the store creator
const myInitialState = { count: 5 };
function App() {
return (
<Provider createStore={() => createStore(myInitialState)}>
<Counter />
</Provider>
);
}
This package was originally inspired by createContext
from zustand/context
that was deprecated in v4. This package is a simplified version of that implementation:
useStoreApi
and forces the use of a selector.useStore
hook to reduce bundle size and improve DX.For a detailed explanation, check out TkDoDo's blog post.
createContext
This is the only export from the package. It creates a context and returns a Provider and useStore
hook.
interface State {
count: number;
}
const [Provider, useStore] = createContext<State>();
Provider
The Provider
component is used to wrap the application and initialize the store.
<Provider createStore={createStore}>
<App />
</Provider>
If you have default props, you can pass them to the Provider
component.
<Provider createStore={() => createStore(myInitialState)}>
<MyComponent />
</Provider>
useStore
The useStore
hook is used to access the store in a child component. Be sure that the child component is wrapped in the Provider
component.
function MyComponent = () => {
const storeState = useStore((state) => state);
return <div>{storeState.count}</div>;
};
FAQs
dependency injection for zustand with react context
The npm package zustand-di receives a total of 155 weekly downloads. As such, zustand-di popularity was classified as not popular.
We found that zustand-di 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
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.