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

@cohook/react

v1.2.0-beta.0

Published

React lightweight, progressive data caching and cross-component communication

Downloads

21

Readme

@cohook/react

React 轻量级、渐进式数据缓存和跨组件通信方案

✨特性

  • 支持数据缓存和共享
  • 支持数据状态化,自动跟踪变化并更新,精准定位作用区间
  • 读写分离,自定义Action去处理数据,灵活化组装实现逻辑复用
  • 轻量,API简单化,类型提示友好
  • 数据不可变
  • 支持插件化拓展功能

📦 安装

yarn add @cohook/react
npm i @cohook/react

⚡快速开始

1. 定义一个Container

import createContainer from "@cohook/react"

const container = createContainer(0)

2. 定义Action

const inc = () => container.commit((draft) => void (draft.current += 1)
const dec = () => container.commit((draft) => void (draft.current -= 1)

3. 数据状态化和修改

function Counter() {
  const count = container.useMapDataToState()
  return (
    <div>
      count:{count}
      <button onClick={inc}>+</button>
      <button onClick={dec}>-</button>
    </div>
  )
}

💡API

createContainer(initialData [, option])

createContainer作为容器工厂函数,接收初始数据initialData和可选的插件和共享等配置option 👉,返回一个容器对象container

container

container.getData()

用于获取当前的容器数据,就像是一个内置的action,可在任何地方使用。

container.useMapDataToState(...overloads)

用于将数据状态化,被状态化后的数据会自动跟踪变化并更新,和React Hooks一样,该API必须在Function Component中使用。

  • const state = container.useMapDataToState()

    • 不带参数,直接将data状态化,
    • 容器内任意数据变化时直接更新
  • const derivedState = container.useMapDataToState(mapStateFn)

    • mapStateFn以data作为参数,可自定义返回的需要状态化的数据
    • 容器内对应状态化数据变化时才会更新,精准定位更新区间

container.commit(updater)

commit 接受一个函数updater(immer中的produce的第二个参数保持一致)作为参数,用来创建修改数据的Action,可灵活组合容器对象提供的方法来组装Action。

/**
 * 1️⃣ Action
 */
const dec = () => {
  const count = container.getData()
  if(count < 0) return
  container.commit((draft) => void (draft.current -= 1)
}

/**
 * 2️⃣ Custom Hooks
 */
function useFilter() {
  const  visibilityFilter = todosContainer.useMapDataToState(data => data.filter)
  const setFilter = (filter: VisibilityFilters) => {
    todosContainer.commit((draft) => {
      draft.current.filter = filter
    })
  }
  
  return {
    filter: visibilityFilter,
    setFilter
  }
}