npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

flooks

v6.4.0

Published

State Manager for React Hooks, Auto Optimized

Downloads

44

Readme

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


React Hooks 状态管理器,可自动优化性能

npm GitHub Workflow Status npm bundle size npm type definitions GitHub

English · 简体中文


特性

  • 惊人的 re-render 自动优化
  • 自动 request loading
  • 极其简单的 API

安装

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

使用

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>
  );
}

* 自动的 request loading - 若函数为异步,asyncFn.loading 为其 loading state。若 asyncFn.loading 未使用,不触发额外 re-render。

示例

Edit flooks

自动优化

flooks 实现了惊人的自动优化,只有真正用到的数据才会注入组件,完全按需 re-render,React 真正变为 "react"。

为什么 flooks 比 zustand 简单?

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

// flooks,无需 selector
// 但也只有 `nuts` 或 `honey` 更新会触发重新渲染,这是自动的
const { nuts, honey } = useStore();

只有函数,无 re-render

const { a } = useStore(); // A 组件,更新 `a`
const { fn } = useStore(); // B 组件,只有函数,无 re-render

无被更新 state,无 re-render

const { a } = useStore(); // A 组件,更新 `a`
const { b } = useStore(); // B 组件,无 `a`,无 re-render

无 fn.loading,无额外 re-render

const { asyncFn } = useStore(); // A 组件,调用 `asyncFn`
asyncFn(); // 无 `asyncFn.loading`,无额外 re-render

// 普通 loading 方案中,即使 `async.loading` 未用到,
// 也会 re-render 至少两次(先 `true` 然后 `false`)。

API

create()

import create from 'flooks';

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

// 对于 `react<=17`,可以使用 `create.config()` 传入
// `ReactDOM.unstable_batchedUpdates` 用于异步更新的批量更新。
//
// create.config({ batch: ReactDOM.unstable_batchedUpdates }); // 在 app 入口处

store()

import create from 'flooks';

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

    store({ a: a + b }); // 使用数据更新 store
    // or
    store((state) => ({ a: state.a + state.b })); // 使用函数更新 store
  },
}));

协议

MIT License (c) nanxiaobei