Socket
Book a DemoInstallSign in
Socket

zustand-context

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zustand-context

Create react contexts with zustand

1.0.0
latest
npmnpm
Version published
Weekly downloads
9K
-36.06%
Maintainers
1
Weekly downloads
 
Created
Source

Zustand Context

Zustand context is a super tiny (281 bytes gzipped) and easy way to create a context-aware zustand store. With this you may determine an initial value for your store and use it in your components with isolated instances.

Creating a Context-aware Zustand Store

type Cat = string

type CatStore = {
  cats: Cat[];
  addCat: (cat: Cat) => void;
  removeCat: (cat: Cat) => void;
}

export const [CatsProvider, useCatsStore] = createZustandContext(
	(initialState: { cats: Cat[] }) =>
		create<CatStore>(set => ({
			cats: initialState.cats,
			addCat: cat => set(state => ({ cats: [...state.cats, cat] })),
      removeCat: cat => set(state => ({ cats: state.cats.filter(c => c !== cat) })),
		})),
);

Consuming a Context-aware Zustand Store

const App = () => {
  return <CatsProvider initialValue={{ cats: ["Salem", "Mimo", "Tapi"] }}>
    <CatsList />
  </CatsProvider>
}

const CatsList = () => {
  /** Exact same api as zustand */
  const cats = useCatsStore(state => state.cats);

  return <ul>
    {cats.map(cat => <li key={cat}>{cat}</li>)}
  </ul>
}

## Contributing

This project is open to contributions. Feel free to open an issue or a pull request.

License

MIT

Keywords

react

FAQs

Package last updated on 30 Apr 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.