Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

🍸 Auto Optimized State Manager for React Hooks

License

NotificationsYou must be signed in to change notification settings

nanxiaobei/flooks

Repository files navigation


flooks

State Manager for React Hooks, Auto Optimized

npmGitHub Workflow Statusnpm bundle sizenpm type definitionsGitHub

English ·简体中文


Features

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

Install

pnpm add flooks# oryarn add flooks# ornpm i flooks

Usage

importcreatefrom'flooks';constuseCounter=create((store)=>({count:0,add(){const{ count}=store();store({count:count+1});},asyncaddAsync(){awaitnewPromise((resolve)=>setTimeout(resolve,1000));const{ add}=store();add();},}));functionCounter(){const{ count, add, addAsync}=useCounter();return(<div><p>{count}</p><buttononClick={add}>+</button><buttononClick={addAsync}>+~{addAsync.loading&&'...'}</button></div>);}

* Automatic request loading - if a function is async,asyncFn.loading is its loading state. IfasyncFn.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 selectorconst{ 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 automaticconst{ 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()

importcreatefrom'flooks';constuseStore=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()

importcreatefrom'flooks';constuseStore=create((store)=>({fn(){const{ a, b}=store();// get statestore({a:a+b});// update state by data// orstore((state)=>({a:state.a+state.b}));// update state by function},}));

License

MIT License (c)nanxiaobei

About

🍸 Auto Optimized State Manager for React Hooks

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors3

  •  
  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp