flooks

Auto Optimized State Manager for React Hooks

README

flooks v6


(Now Support React 18)

State Manager for React Hooks, Auto Optimized
npm GitHub Workflow Status Codecov npm bundle size npm type definitions GitHub

English · 简体中文



Features


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

Install


  1. ```sh
  2. yarn add flooks

  3. # npm i flooks
  4. ```

Usage


  1. ``` js
  2. import create from 'flooks';

  3. const useCounter = create((store) => ({
  4.   count: 0,
  5.   add() {
  6.     const { count } = store();
  7.     store({ count: count + 1 });
  8.   },
  9.   async addAsync() {
  10.     await new Promise((resolve) => setTimeout(resolve, 1000));
  11.     const { add } = store();
  12.     add();
  13.   },
  14. }));

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

  17.   return (
  18.     <div>
  19.       <p>{count}</p>
  20.       <button onClick={add}>+</button>
  21.       <button onClick={addAsync}>+~ {addAsync.loading && '...'}</button>
  22.     </div>
  23.   );
  24. }
  25. ```

*\ 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?


  1. ``` js
  2. // zustand, need a selector
  3. const { nuts, honey } = useStore((state) => ({
  4.   nuts: state.nuts,
  5.   honey: state.honey,
  6. }));

  7. // flooks, no selector needed
  8. // but also only `nuts` or `honey` update triggers re-render, it's automatic!
  9. const { nuts, honey } = useStore();
  10. ```

Only functions, no re-render


  1. ``` js
  2. const { a } = useStore(); // A component, update `a`
  3. const { fn } = useStore(); // B component, only functions, no re-render
  4. ```

No updated state, no re-render


  1. ``` js
  2. const { a } = useStore(); // A component, update `a`
  3. const { b } = useStore(); // B component, no `a`, no re-render
  4. ```

No \*.loading, no extra re-render


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

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

API


create()


  1. ``` js
  2. import create from 'flooks';

  3. const useStore = create((store) => storeData);

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

store()


  1. ``` js
  2. import create from 'flooks';

  3. const useStore = create((store) => ({
  4.   fn() {
  5.     const { a, b } = store(); // get store

  6.     store({ a: a + b }); // update store by payload
  7.     // or
  8.     store((state) => ({ a: state.a + state.b })); // update store by function
  9.   },
  10. }));
  11. ```

License



FUTAKE


Try [FUTAKE](https://sotake.com/f) in WeChat. A mini app for your inspiration moments. 🌈

undefined