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

@wymjs/react-atom

v1.0.4

Published

極簡 react18+ 原子狀態管理

Downloads

41

Readme

@wymjs/react-atom

極簡 react18+ 原子狀態管理

安裝

# react 為關聯依賴
pnpm i @wymjs/react-atom react

使用

import { atom } from '@wymjs/react-atom'

// 申明原子狀態
const countAtom = atom<number>(0)
// 可以使用 get => T 的方式申明,get 方法傳入其他的原子將達成派生效果
const doubleCountAtom = atom<number>(get => get(countAtom) * 2)

// 返回的為暫停函數,若寫在 useEffect 中可以直接 return countAtom.watch(...)
const stop = countAtom.watch((before, after) => {
  // 可以看到 doubleCountAtom 的值也是跟隨著 after 變化的
  console.log(`[countAtom watch] before: ${before}, after: ${after}, doubleCount: ${doubleCountAtom.value}`)
  
  if (after > 10) {
    alert('已經加超過 10,將取消監聽')
    stop()
    return
  }
  
  if (after % 3 === 0) {
    alert('每到 3 的倍數我就出現一次')
  } 
})

function DefaultCount() {
  // 若不想監聽變化可以使用 .value 取值
  return <div>不監聽變化的 count: {countAtom.value}</div>
}

function CountButton() {
  // 若要在組件中追蹤狀態變化可以使用 .use 以 hook 的方式取出
  // 因為是 hook,得照 react hook 的規則在組件頂部使用
  const count = countAtom.use()

  function onIncrease() {
    // 可以使用以下兩種方式來更新值,類似 react 的 setState
    countAtom(e => e + 1)
    // countAtom(countAtom.value + 1)
  }
  
  return (
    <div>
      <button onClick={onIncrease}>count: {count}</button>
    </div>
  )
}

function DoubleCount() {
  const doubleCount = doubleCountAtom.use()
  
  return (
    <div>doubleCount: {doubleCount}</div>
  )
}

export function Counter() {
  return (
    <>
      <DefaultCount />
      <DoubleCount />
      <CountButton />
    </>
  )
}

API

  • atom(T) 返回 Proxy(下做 p) 申明一個原子狀態
  • atom((get: (atom: Atom<T>) => T) => T) 返回 Proxy(下做 p) 使用 get 取得其他原子的值並關聯變化
  • p
    • p(T) 返回 void 以傳入的值替換將原本的值
    • p((before: T) => T) 返回 void 以函數的方式將返的的值替換將原本的值
    • .use() 返回 T 為在組件取值並監聽值變化而重新渲染組件
    • .value 返回 T 取出當前值(其實實現裡沒判斷是否為 value key,只是類型強寫得而已)
    • .watch(listener: (before: T, after: T) => void) 返回 () => void 監聽數據變化,返回的函數為暫停