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

@efox/imook

v1.1.0

Published

基于[immer](https://immerjs.github.io/immer/docs/introduction)和[react hooks](https://reactjs.org/docs/hooks-intro.html)的轻量的局部状态管理,类比custom hook,@efox/imook强调基于功能设计store。

Downloads

23

Readme

@efox/imook

基于immerreact hooks的轻量的局部状态管理,类比custom hook,@efox/imook强调基于功能设计store。

✨特性

  • 局部状态管理,精准定位store作用区间
  • 无this, 状态不可变
  • 支持异步action,修改状态简单化
  • 支持读写分离和定制更新(衍生数据)
  • 支持中断更新和批量更新同步逻辑

📦 安装

npm i @efox/imook
yarn add @efox/imook

⚡快速开始

1. 定义一个local store

import React from "react"
import { createLocalStore, ActUtil } from "@efox/imook";

// 创建counterStore
const initialState: number = 0
type CounterActUtil = ActUtil<number>;

export default createLocalStore(initialState, {
  inc({ commit }: CounterActUtil, step = 1) {
    commit((draftStore) => {
      draftStore.state += step;
    })
  },
});

2. 使用和修改状态

import counterSotre from "./counterStore"

function Counter() {
  // 返回[state, actions]
  const [count, { inc }] = counterStore.useStore()
  return (
    <div>
      count:{count}
      <button onClick={inc}>+</button>
    </div>
  )
}

export default function Counter() {
  return (
    <counterStore.Provider>
      <Counter />
    </counterStore.Provider>
  )
}

💡API

createLocalStore(initialState, actionCreators)

createLocalStore接收初始状态initialState和用于修改状态的actionCreators,返回一个localStore对象

actionCreators

action生成器对象,属性为任意的函数,函数第一个参数默认为ActUtil(action的工具类)。

import { ActUtil } from "@efox/imook";
const actionCreators = {
  actionName({commit, stateRef}: ActUtil<StateType>, ...args: any[]) {
    /** 
     * stateRef存储当前state的Ref
     */

   // stateRef.current...

    /**
     * commit 用于修改状态, 接受一个函数updater作为参数
     * updater和immer中的produce的第二个参数保持一致
     * 通过操作draftStore.state完成对state的修改
     */
    commit((draftStore) => {
      // draftStore.state...
    });
    // ...
  }
}

localStore

局部的store, 包含作用区间的Provider和对应的hooks方法;多个store之间可以通过imook暴露的withStores或Provider进行自动组合。

import {createLocalStore, Provider, withStores} from "@efox/imook"

const localStore = createLocalStore(initialState, actionCreators [, subscriptions])
// 需要读写状态的组件使用useStore, 返回一个元组,包含state和actions对象
function Component() {
  const [state, actions] = localStore.useStore()
  // ...
}

// 只需要写状态的组件使用useActions, 返回一个actions对象,组件不会触发重渲染
function Component() {
  const actions = localStore.useActions()
  // ...
}

// 通过useSubscribe获取订阅项,参数为subscriptions的属性名
function Component() {
  const xxx = localStore.useSubscribe('xxx')
  // ...
}

// 不需要组合stores,直接用localStore提供的Provider包裹Component即可
export default function App() {
  return (
    <localStore.Provider>
     <Component />
    </localStore.Provider>
  )
}

// 组合stores
const stores = [localStore]

// 方式1:使用Provider
export default function App() {
  return (
    <Provider stores={stores}>
     <Component />
    </Provider>
  )
}
// 方式2:使用withStores
function App() {
  return (
     <Component />
  )
}
export default withStores(App, stores)