Skip to content

🧘 An extremely simple react store works with React.useSyncExternalStore

License

Notifications You must be signed in to change notification settings

JASONPANGGO/zen-store

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zen-store

NPM version

An extremely simple react store works with React.useSyncExternalStore

  • Zero dependency.

Install

npm i @jasonpang/zen-store -S

Usage

import React, { useSyncExternalStore } from "react";
import ZenStore from 'zen-store';

const Store = new ZenStore(0);

function Button() {
    const handleClick = () => {
        Store.update(Store.getSnapshot() + 1)
    }
    return <button role="button" onClick={handleClick}>add</button>
}

function Counter() {
    const count = useSyncExternalStore(Store.subscribe.bind(Store), Store.getSnapshot.bind(Store));

    return <div role="counter">
        <i role="count">{count}</i>
        <Button />
    </div>
}

About

🧘 An extremely simple react store works with React.useSyncExternalStore

Resources

License

Stars

Watchers

Forks

Packages

No packages published