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-fractal

v1.0.2

Published

<p align="center"> <img src="https://img.alicdn.com/imgextra/i2/O1CN011N4AN11QnfRWmTt2D_!!6000000002021-0-tps-1024-1024.jpg" alt="Zustand Fractal Logo" /> </p>

Downloads

144

Readme

zustand-fractal

Version Downloads

zustand-fractal is a library that allows you to create modular, composable and fractal state management with Zustand. It combines the simplicity and power of Zustand with the flexibility of fractal state architecture.

zustand-fractal can easily handle the issue of multiple instances of Redux Store, as well as the problem of how to reuse components that have consumed specific data from the Redux Store through useStore in other scenarios.

Installation

npm install zustand-fractal
# or
yarn add zustand-fractal

Why zustand-fractal?

  • Enables modular and composable state management
  • Promotes reusability of state slices across different parts of your application
  • Maintains the performance benefits of Zustand
  • Allows for easy organization of complex state logic

Basic Usage

First, create a fractal store:

import { createStore } from "zustand-fractal";

export const BearStore = createStore(
  () => ({
    bears: 0,
  }),
  (set) => ({
    increasePopulation: () => set((state) => state.bears++),
  })
);

Then use it in your components:

import { useStore, useActions } from "zustand-fractal";

function BearComponent() {
	const bears = useStore(BearStore, (state) => state.bears);
	const bearsActions = useActions(BearStore);

	return (
		<div>
			<h1>{bears} bears</h1>
			<button onClick={bearsActions.increasePopulation}>increase</button>
		</div>
	);
}

Advanced Usage

Nested Fractal Stores

Zustand Fractal allows you to create nested fractal stores for more complex state management:

const BearsManagerStore = createStore(() => ({
	red: {
		bears: 0,
	},
	green: {
		bears: 1,
	},
	blueList: []
}), (set) => ({
	addBlueBear: (bearsCnt: number) => set(state => state.blueList.push({ bears: bearsCnt })),
	updateRed: (redBear) => set(state => state.red = readBear),
	updateGreen: (greenBear) => set(state => state.green = greenBear),
	updateBlue: (blueBear, index) => set(state => state.blueList[index] = blueBear),
}))

function BearsManager() {
	const bearsManager = useStore(BearsManagerStore);
	const bearsManagerActions = useActions(BearsManagerStore);

	return (
		<div>
			<h1>red: </h1>
			<BearStore.Provider
				rootStore={BearsManagerStore}
				selector={state => state.red}
				updator={bearsManagerActions.updateRed}
			>
				<BearComponent />
			</BearStore.Provider>


			<h1>green: </h1>
			<BearStore.Provider
				rootStore={BearsManagerStore}
				selector={state => state.green}
				updator={bearsManagerActions.updateGreen}
			>
				<BearComponent />
			</BearStore.Provider>

			<h1>blue: </h1>
			{
				bearsManager.blueList.map((blueItem, index) => {
					return (
						<BearStore.Provider
							key={index}
							rootStore={BearsManagerStore}
							selector={state => state.blueList[index]}
							updator={(newBlue) => {
								bearsManagerActions.updateBlue(newBlue, index);
							}}
							deps={[]}
						>
							<BearComponent />
						</BearStore.Provider>
					)
				})
			}
			<button onClick={() => bearsManagerActions.addBlueBear(0)}>add bears</button>
		</div>
	);
}

Computed Properties

const TaskStore = createFractalStore(
  () => ({
    totalTasks: 0,
    completedTasks: 0,
  }),
  (set, get) => ({
    progressPercentage() {
      return get().totalTasks === 0
        ? 0
        : (get().completedTasks / get().totalTasks) * 100;
    },
  })
);

Middleware Support

Zustand Fractal supports Zustand middleware. Here's an example using the persist middleware:

import { persist, devtools } from "zustand/middleware";
import { createStore } from "zustand-fractal";

const PersistentBearStore = createStore(
  devtools(
    persist(
      immer(() => ({
        bears: 0,
      })),
      { name: "bear-storage" }
    )
  ),
  (set) => ({
    addBear: () => set((state) => ({ bears: state.bears + 1 })),
  })
);

TypeScript Support

Zustand Fractal is written in TypeScript and provides full type support out of the box.

Contributing

We welcome contributions!

License

Zustand Fractal is MIT licensed.