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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@jafish/store-in-react

v2.0.2

Published

m-store在React下的扩展

Readme

@jafish/store-in-react

npm

m-store在react下的扩展,提供同步数据的api

使用

import MStore from '@jafish/m-store'
import { createStorer, createStorerInHook } from '@jafish/store-in-react'

const store = new MStore({
  test: 1,
})

// 自增
const addTest = () => store.set(state => ({ test: state.test + 1 }))

// 囊括组件的方式
const Node1 = createStorer(
  [store, state => ({
    test: state.test,
  })],
  ...可以更多,参数格式一致
)(
  // ({ test }) => <div onClick={addTest}>{test}</div>
  class extends Component {
    render() {
      const { test } = this.props

      return <div onClick={addTest}>{test}</div>
    }
  }
)

// HOOK的方式
const useTestStore = createStorerInHook(store, state => ({
  test: state.test,
}))

const Node2 = () => {
  const { test }  = useTestStore()

  return <div>{test}</div>
}

API

createStorer([store, getState], ...)(Node)

第一个函数接收多个参数,参数格式一致:[store实例, 获取状态的回调]

返回的函数接收一个返回状态的组件,可以是函数式组件,可以是类组件

const Node1 = createStorer(
  [store, state => ({
    test: state.test,
  })],
  [store, state => ({
    test2: state.test,
  })],
)(class extends Component {
  async componentDidMount() {
    console.log(this.props)
    
    await addTest()
    // 通过 await ,可以立即拿到变更后的 state
    console.log(this.props)
  }

  render() {
    const { test, test2 } = this.props

    return <div onClick={addTest}>{test} {test2}</div>
  }
})

createStorerInHook(store, getState): () => state

该方法仅可用在函数式组件,内部使用了 useState useEffect。

接收两个参数,第一个是store实例,第二个是获取状态的回调,返回一个在函数式组件内部使用的 function,执行后返回状态

// HOOK的方式
const useTestStore = createStorerInHook(store, state => ({
  test: state.test,
}))

const useTestStore2 = createStorerInHook(store, state => ({
  test: state.test,
}))

const Node2 = () => {
  const { test }  = useTestStore()
  const testStore2 = useTestStore2()

  return <div>{test} {testStore2.test}</div>
}

实践

细化状态的方式可以让我们达到更低的渲染成本,状态尽量细化到小组件内,外部仅负责调用

将状态与页面分离

store.js

import MStore from '@jafish/m-store'

export const store = new MStore({
    test: 1
})

export const addTest = async () => {
    const { test } = store.get()

    if (test % 2 === 0) await new Promise(resolve => setTimeout(resolve, 500))

    store.set(state => ({
        test: state.test + 1
    }))
}

index.jsx

import React from 'react'
import { createStorer, createStorerInHook } from '@jafish/store-in-react'
import { store as TestStore, addTest } from './store'

const useTestStore = createStorerInHook(TestStore, state => ({
    test: state.test
}))

const Add = () => {
    const { test } = useTestStore()

    return <div>{test}</div>
}

export default function App() {

    return (
        <div onClick={addTest}>
            <Add />
        </div>
    )
}

更新

2.0.0

将 react 版本提升到 17

将 useStore 改为 createStorer

将 useStoreInHook 改为 createStorerInHook

规避 hook 的规则验证