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

@ezpzee/store

v0.5.0

Published

## Scope Create a straight forward way to manage the app global store and allow communication between components and/or services.

Downloads

2

Readme

Ezpzee Store Manager

Scope

Create a straight forward way to manage the app global store and allow communication between components and/or services.

Usage

Setup

First I recommend declaring a global store type. This will help you down the line to know what values to expect into your store and how to use or process them.

types.ts

export type GlobalStoreType = {
  currentUser: {
    id: string;
    name: string;
    email: string;
  }
}

Now let's write our first global store module AuthModule.ts

import { StoreProvider, ModuleType, ReducerType } from '@ezpzee/store'
import { GlobalStoreType } from './types'

// use Symbol to make sure you have unique action identifiers
export const AuthActions  = {
  SUCCESS: Symbol('AUTH_SUCCESS') 
}

// using a store type with the ReducerType will add intelisense on prevStore argument
const AuthReducer: ReducerType<GlobalStoreType> = (prevStore, action, payload) => {
  switch (action) {
    case AuthActions.SUCCESS:
      return {...prevStore, currentUser: payload}

    default:
      return prevStore
  }
}

export default {
  actions: AuthActions,
  reducer: AuthReducer,
  initialState: {}
}

Finally we can now setup the store provider at the top level of our app.

import { StoreProvider, ModuleType } from '@ezpzee/store'
import AuthModule from './AuthModule'

import { App } from './App'

const modules: Array<ModuleType> = [
  AuthModule
]

<StoreProvider modules={modules}>
  <App />
</StoreProvider>

And that's all for the setup. Easy peasy right?

Dispatching actions

You have two ways of dispatching an action:

Using useGlobalStore hook:

import { useGlobalStore } from '@ezpzee/store'
import { AuthActions } from './AuthModule'

export const SampleComponent = () => {
  const { dispatch } = useGlobalStore()

  const auth = async (username, pass) => {
    const response = await authApiCall(username, pass)
    dispatch(AuthActions.SUCCESS, response)
  }

  return ...
}

or using dispatchStoreAction function:

import { dispatchStoreAction } from '@ezpzee/store'
import { AuthActions } from './AuthModule'

export const AuthFunction = (username, pass) => {
  const response = await authApiCall(username, pass)
  dispatchStoreAction(AuthActions.SUCCESS, response)
}

Both functions return the new store after reducers have been executed. You can also use const newStore = dispatch<StoreType>(action, payload) for better intelisense.

What about async actions?

I do not plan to integrate async functionality. The scope of the library is just to manage the global scope. Any async processes should take place before dispatching the action with the final payload.

Accesing the store

Same as the dispatch you have two ways of accessing the store:

Using useGlobalStore hook:

import { useGlobalStore } from '@ezpzee/store'
import { GlobalStoreType } from './types'

export const HeaderComponent = () => {
  // Store type is optional, is just gives better intelisense for the store property
  const { store } = useGlobalStore<GlobalStoreType>()
  const { currentUser } = store;

  return <>Hello {currentUser.name}</>
}

or using getGlobalStore function:

import { getGlobalStore } from '@ezpzee/store'
import { GlobalStoreType } from './types'

export const HelperFunction = () => {
  // Store type is optional, is just gives better intelisense for the store property
  const store = getGlobalStore<GlobalStoreType>()

  return currentUser.name
}