Socket
Socket
Sign inDemoInstall

zustand-signal

Package Overview
Dependencies
4
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    zustand-signal

Another React binding for Zustand


Version published
Weekly downloads
3
increased by50%
Maintainers
1
Install size
209 kB
Created
Weekly downloads
 

Changelog

Source

[0.5.3] - 2023-02-26

Changed

  • fix(jsx-runtime): revert jsxDEV #12

Readme

Source

zustand-signal

CI npm size discord

Another React binding for Zustand

What it is

Typically, Zustand store is a React hook you can just use in React. There's alternative library called use-zustand.

This library provides yet another method. It follows jotai-signal, which is inspired by @preact/signals-react.

It allows to use the Zustand store in React without using hooks. We don't need to follow the rules of hooks.

How to use it

/** @jsxImportSource zustand-signal */

import createStore from 'zustand/vanilla';
import { $ } from 'zustand-signal';

const store = createStore((set) => ({
  count: 0,
  inc: () => set((state) => ({ count: state.count + 1 })),
}));

setInterval(() => {
  store.getState().inc();
}, 100);

const Counter = () => (
  <div>
    Count: {$(store).count}
  </div>
);

How it works

The pragma at the first line does the trick. It will transform the code with signal to the code that React can handle.

Original code

/** @jsxImportSource zustand-signal */

const Counter = () => (
  <div>
    Count: {$(store).count} ({Math.random()})
  </div>
);

Pseudo transformed code

import { useEffect, useMemo, useReducer } from 'react';

const Counter = () => {
  const [, rerender] = useReducer((c) => c + 1, 0);
  useEffect(() => {
    let lastValue;
    const unsubscribe = store.subscribe(() => {
      const nextValue = store.getState().count;
      if (lastValue !== nextValue) {
        lastValue = nextValue;
        rerender();
      }
    });
    return unsubscribe;
  }, []);
  return (
    <div>
      {useMemo(() => 'Count: '), []}
      {store.getState().count}
      {useMemo(() => ` (${Math.random()})`, [])}
    </div>
  );
};

Keywords

FAQs

Last updated on 25 Feb 2023

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc