• Stars
    star
    509
  • Rank 86,772 (Top 2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 3 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿชข The simplest React state manager

๐Ÿชข resso

The Simplest React State Manager

Auto on-demand re-render โšก๏ธ


Reactive Elegant Shared Store Object

(Support React 18, React Native, SSR, Mini Apps)

npm GitHub Workflow Status Codecov npm bundle size npm type definitions GitHub

English ยท ็ฎ€ไฝ“ไธญๆ–‡


Introduction

resso, worldโ€™s simplest React state manager โ†’

Features

  • Extremely simple ๐Ÿชฉ
  • Extremely smart ๐Ÿซ™
  • Extremely small ๐Ÿซง

Install

pnpm add resso
# or
yarn add resso
# or
npm i resso

Usage

import resso from 'resso';

const store = resso({ count: 0, text: 'hello' });

function App() {
  const { count } = store; // data in UI must destructure at top first ๐Ÿฅท
  return (
    <>
      {count}
      <button onClick={() => ++store.count}>+</button>
    </>
  );
}

Edit resso

API

Initialize

import resso from 'resso';

const store = resso({
  count: 0,
  inc: () => {
    const { count } = store; // data in methods must destructure at top, also ๐Ÿฅท
  },
});

Update

// single update โ†’ directly assign
store.count = count + 1;

// single update โ†’ updater funtion
store('count', (prev) => prev + 1);

// multiple updates
Object.assign(store, { a, b, c });

Use

// data in UI must destructure at top first, since they were injected by useState
function App() {
  const { count } = store; // must at top, or may get React warning (Hooks rules)
}

* react<18 batch update

// to use batch update when react<18:
resso.config({ batch: ReactDOM.unstable_batchedUpdates }); // at app entry

Re-render on demand

// no text update, no re-render
function Text() {
  const { text } = store;
  return <p>{text}</p>;
}

// only when count updates, re-render
function Count() {
  const { count } = store;
  return <p>{count}</p>;
}

// no data in UI, no re-render
function Control() {
  return (
    <>
      <button onClick={store.inc}>+</button>
      <button onClick={() => --store.count}>-</button>
    </>
  );
}

License

MIT License (c) nanxiaobei

FUTAKE

Try FUTAKE in WeChat. A mini app for your inspiration moments. ๐ŸŒˆ