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

next-context-store

v2.0.2

Published

use new context api as store | 使用新的context api做数据仓库

Downloads

8

Readme

next-context-store

use new context api as store | 使用新的context api做数据仓库

  • redux is single store, I want some simple multi store | redux 是单仓库的,我需要一个简单的多仓库
  • new context api is rather an injector than store | 相比store新的context api更像是一个injector,作为store使用并不方便

so I start this, for at times I need small stores to hold shered data cross components, for example store data only for shared in parts of one page, and I don't want all the data for every page to go inside redux | 所以我开始了这个项目,因为有时候我需要用一些小仓库来保持跨组件的数据,例如仅限某个页面使用的数据,我并不想让所有页面的数据都放到 redux

try it out: https://codesandbox.io/s/next-context-store-50sjq

basic useage | 基础使用

import { createStore } from 'next-context-store'

const CounterCtx = createStore(0)
export const Counter = () => {
  return <CounterCtx.Provider> 
    <CounterCtx.Consumer>{
      ([loading, value, error, dispatch]) => <div>
        <pre>{
          JSON.stringify({ loading, value, error }, null, 2)}</pre>
        <button onClick={() => dispatch(x => x + 1)}>+1</button>
        <button onClick={() => dispatch(async () => {
          await new Promise(r => setTimeout(r, 1000))
          return x => x + 1
        })}>+1 async</button>
      </div>
    }</CounterCtx.Consumer>
  </CounterCtx.Provider>
}

const UserCtx = createStore('')
export const Login = () => {
  let [loading, user, error, dispatch] = UserCtx.useContext()
  return <UserCtx.Provider>
    {user ? <>
      {user}|<button onClick={() => dispatch("")}>logout</button>
    </> : <button onClick={() => dispatch(async () => {
      await new Promise(r => setTimeout(r, 1000))
      return 'someone'
    })}>login</button>}|{loading ? '...loading' : ''}
  </UserCtx.Provider>
}

example usage | 使用示例: ./examples/basic/

APIs

createStore(initialValue,initialAction)

params

initialValue: T extends any initial value of store

initialAction: Action<T> optional, initial action

return value: obj object

obj.Provider: FC<{}> domain this store works

obj.Consumer : React.Consumer<[loading: boolean, value: T, error: any, dispatch: (fn: Action<T>) => void]>* to use the store

obj.useContext: ()=>[loading: boolean, value: T, error: any, dispatch: (fn: Action<T>) => void] hook to use this store

obj.Context: React.Context<[loading: boolean, value: T, error: any, dispatch: (fn: Action<T>) => void]> React Context if you need

about Action:

type Action<T> = T | ((v?: T) => T)
  | Promise<T> | ((v?: T) => Promise<T>)
  | ((val?: T) => Promise<(v?: T) => T>)