flooks

Auto Optimized State Manager for React Hooks
GitHub
351
Created 5 years ago, last commit 5 months ago
3 contributors
327 commits
Stars added on GitHub, month by month
0
0
0
0
0
12
1
2
3
4
5
6
7
8
9
10
11
2023
2024
Stars added on GitHub, per day, on average
Yesterday
=
Last week
0.0
/day
Last month
0.0
/day
Last 12 months
0.0
/day
npmPackage on NPM
flooks
6.4.0
Monthly downloads on NPM
12
1
2
3
4
5
6
7
8
9
10
11
2023
2024
README

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


flooks

State Manager for React Hooks, Auto Optimized

npm GitHub Workflow Status npm bundle size npm type definitions GitHub

English · 简体中文


Features

  • Gorgeous auto optimized re-render
  • Automatic request loading
  • Extremely simple API

Install

pnpm add flooks
# or
yarn add flooks
# or
npm i flooks

Usage

import create from 'flooks';

const useCounter = create((store) => ({
  count: 0,
  add() {
    const { count } = store();
    store({ count: count + 1 });
  },
  async addAsync() {
    await new Promise((resolve) => setTimeout(resolve, 1000));
    const { add } = store();
    add();
  },
}));

function Counter() {
  const { count, add, addAsync } = useCounter();

  return (
    <div>
      <p>{count}</p>
      <button onClick={add}>+</button>
      <button onClick={addAsync}>+~ {addAsync.loading && '...'}</button>
    </div>
  );
}

* Automatic request loading - if a function is async, asyncFn.loading is its loading state. If asyncFn.loading is not used, no extra re-render.

Demo

Edit flooks

Auto optimization

flooks realizes a gorgeous auto optimization, only actually used data will be injected into the component, re-render completely on demand, when React is truly "react".

Why flooks over zustand?

// zustand, need a selector
const { nuts, honey } = useStore((state) => ({
  nuts: state.nuts,
  honey: state.honey,
}));

// flooks, not need a selector
// but also only `nuts` or `honey` update trigger re-render, it's automatic
const { nuts, honey } = useStore();

Only functions, no re-render

const { a } = useStore(); // A component, update `a`
const { fn } = useStore(); // B component, only functions, no re-render

No updated state, no re-render

const { a } = useStore(); // A component, update `a`
const { b } = useStore(); // B component, no `a`, no re-render

No fn.loading, no extra re-render

const { asyncFn } = useStore(); // A component, call `asyncFn`
asyncFn(); // No `asyncFn.loading`, no extra re-render

// With normal loading solutions, even `asyncFn.loading` is not used,
// it will re-render at least twice (turn `true` then `false`).

API

create()

import create from 'flooks';

const useStore = create((store) => obj);

// For `react<=17`, you can use `create.config()` to pass
// `ReactDOM.unstable_batchedUpdates` for batch updating in async updates.
//
// create.config({ batch: ReactDOM.unstable_batchedUpdates }); // at app entry

store()

import create from 'flooks';

const useStore = create((store) => ({
  fn() {
    const { a, b } = store(); // get state

    store({ a: a + b }); // update state by data
    // or
    store((state) => ({ a: state.a + state.b })); // update state by function
  },
}));

License

MIT License (c) nanxiaobei