npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zustand-di

v0.0.16

Published

dependency injection for zustand with react context

Downloads

783

Readme

zustand-di

dependency injection for zustand with react context. initialize zustand stores with props.

Build Size Version Downloads

Installation

npm install react zustand zustand-di

Note: zustand-di requires react and zustand as peer dependencies.

For [email protected] and higher, you need [email protected] or higher.

For zustand@~4.0.0, you need [email protected] or lower.

Usage

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>
  );
}

Motivation

This package was originally inspired by createContext from zustand/context that was deprecated in v4. This package is a simplified version of that implementation:

  • Removes useStoreApi and forces the use of a selector.
  • Uses modern typescript features to simplify the code and reduce bundle size.
  • Returns an array of the Provider and useStore hook to reduce bundle size and improve DX.

For a detailed explanation, check out TkDoDo's blog post.

Why is Dependency Injection useful within React?

  • You can pass in props to the store creator.

API

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>;
};