Git Product home page Git Product logo

resso's Introduction

Link in bio to widgets, your online home screen. ➫ 🔗 kee.so


🪢 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 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
bun add resso
# or
npm i resso

Usage

import resso from 'resso';

const store = resso({
  count: 0,
  text: 'hello',
  inc() {
    const { count } = store; // must destructure at top (if use in method)
    store.count = count + 1;
  },
});

function App() {
  const { count } = store; // must destructure at top (if use in UI)

  return (
    <>
      {count}
      <button onClick={() => (store.count += 1)}>+</button>
    </>
  );
}

* destructure at top is calling useState (Hooks rules, or may get React error)

Edit resso

API

Single update

store.count = 60;

store('count', (c) => c + 1);

Multiple update

store({
  count: 60,
  text: 'world',
});

store((s) => ({
  count: s.count + 1,
  text: s.text === 'hello' ? 'world' : 'hello',
}));

Non-state shared vars (Refs)

Actually it's not related to resso, it's just JavaScript. You can do it like this:

// store.js
export const refs = {
  total: 0,
};

// App.js
import store, { refs } from './store';

function App() {
  refs.total = 100;
  return <div />;
}

* react<18 batch update

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 state in UI, no re-render
function Control() {
  return (
    <>
      <button onClick={store.inc}>+</button>
      <button onClick={() => (store.count -= 1)}>-</button>
    </>
  );
}

License

MIT License (c) nanxiaobei

resso's People

Contributors

himself65 avatar nanxiaobei avatar tyleratcodeoflife avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

resso's Issues

Nested objects?

Hey,

First of all great job :)

just have a question about nested object like this one:

const store = resso({
  user: {
    info: {
      count: 0,
    }
  },
  inc: () => store.user.info.count++,
});

function Control() {
  const { inc } = store;
  return (
    <>
      <p>{store.user.info.count}</p>
      <button onClick={inc}>-</button>
    </>
  );
}

Maybe this line https://github.com/nanxiaobei/resso/blob/main/src/index.ts#L31 should be some recursive function?
I'm not so good so ... I don't have any PR or suggestions ... just try to learn :) Thanks in advance!

orbs: android: circleci/[email protected] node: circleci/[email protected] jobs: test-fastlane: docker: - image: cimg/android:2022.07 auth: username: mydockerhub-user password: $DOCKERHUB_PASSWORD # context / project UI env-var reference resource_class: large steps: - checkout - node/install: install-yarn: false node-version: "16.13.0" - run: npm install - android/decode-keystore: keystore-location: android/app/keystore - android/create-keystore-properties: working-directory: android - android/create-google-play-key: working-directory: android - android/fastlane-deploy: working-directory: android lane-name: internal

limitation of resso

I found below snippet would not work:

import { useState } from 'react';
import resso from 'resso';

// 🪢 resso
// https://github.com/nanxiaobei/resso

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

function App() {
  const [i, setI] = useState(0);

  if (i % 2 === 1) {
    return <div>i === {i}</div>;
  }

  return (
    <>
      <div>{store.count}</div>
      <button
        onClick={() => {
          store.inc();
          setI(i + 1);
        }}
      >
        inc
      </button>
    </>
  );
}

export default App;

Check codesandbox.

It will cause the error Rendered fewer hooks than expected. This may be caused by an accidental early return statement.

We can change it a little bit as below:

import { useState } from 'react';
import resso from 'resso';

// 🪢 resso
// https://github.com/nanxiaobei/resso

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

function App() {
  const { count } = store;
  const [i, setI] = useState(0);

  if (i % 2 === 1) {
    return <div>i === {i}</div>;
  }

  return (
    <>
      <div>{count}</div>
      <button
        onClick={() => {
          store.inc();
          setI(i + 1);
        }}
      >
        inc
      </button>
    </>
  );
}

export default App;

The user should eject the value from store before any of condition statement.

I suggest to put such info as notice/usage-limitation in readmd, so that users are able to avoid such issue

hook数量不一致疑问

大佬这个库确实大道至简,非常棒!
但是这个用provy代理get,实际上是执行一个hook,开发者不会把它当成是hook去注意,可能会不经意间放到条件语句里面,导致错误
image
就像这种情况,感觉有点美中不足😂

不能在函数内部使用this

demo里给的示例是这样的:

const store = resso({
  user: {
    info: {
      count: 0,
    }
  },
  inc: () => store.user.info.count++,
});

我尝试下面的写法,却不能正常运行:

const store = resso({
      count: 0,
  },
  inc(){ this.count++}
});

会报错:TypeError: Cannot set properties of undefined (setting 'count')

this 未定义

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.