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

gudam

v1.0.0

Published

Gudam is a tiny store management library for react.

Downloads

2

Readme

Gudam (গুদাম): Simplifying React State Management

Gudam Banner

Introduction

Gudam stands as a robust yet lightweight React state management library, designed to elevate your application's performance. With an impressively small footprint of just 1kb, it boasts quick execution, straightforward implementation, and scalability. This TypeScript-based library ensures reliable and statically-typed state management for your React applications.

Installation

Getting started with Gudam is a breeze. Simply install it using npm with the following command:

npm install gudam

Setting Up Gudam

Integrating Gudam into your root component is a straightforward process. Follow these steps:

import { GudamContext, useGudam } from 'gudam'

function App() {
  const gudam = useGudam()
  return (
    <GudamContext.Provider value={gudam}>
      {/* Your application is now powered by Gudam */}
      <div>Your App Content</div>
    </GudamContext.Provider>
  )
}

export default App

Creating a Store

Define a store in Gudam using the defineStore function. The following example illustrates how to define a store for authentication, including additional details:

import { defineStore } from 'gudam'

export const authGudam = defineStore(
  'auth', // Unique key
  {
    // states
    token: '', // Authentication token
    role: '', // User role
    profile: {
      firstName: '', // User's first name
      lastName: '', // User's last name
    },
    // accessors
    get isLoggedIn() {
      return !!this.token && !!this.role; // Check if the user is logged in
    },
    get greetingMessage() {
      return this.profile.firstName
        ? `Welcome back ${this.profile.firstName}`
        : 'Welcome Guest';
    },
    // methods
    setAuth(token, role) {
      this.token = token;
      this.role = role;
    },
    setFirstName(name: string) {
      this.profile = { ...this.profile, firstName: name };
    },
  },
  {
    version: '0.0.1', // Version of the store
    storage: localStorage, // Storage engine (localStorage or sessionStorage)
    parse: JSON.parse, // Parser function to parse from string to store state
    stringify: JSON.stringify, // Converts stateful properties of your store to JSON string
  }
);

defineStore accepts three parameters:

  1. key: A unique identifier for your store.

  2. store: Your store object with states, accessors, and methods.

    • states: Non-function-type writable properties are states. In our authGudam store, we have token, role, and profile states.
    • accessors: Accessors are functions with the get identifier, providing a way to access computed or derived properties from the state.
    • methods: Methods are pure JavaScript functions. You can use these functions to make changes to your store states.

    The store object is a regular JavaScript object, except that it triggers effects if changes are made to any state property.

  3. persistOptions (optional): An object with persist options.

    | Property | Description | Default Value | | --------- | ----------------------------------------------------------------------------------------------------- | -------------- | | storage* | Storage engine (localStorage or sessionStorage). It is required to enable persistence | undefined | | version | Version of your current store. Change the version to invalidate old store values in the browser cache | 0.0.1 | | parse | Parser function to convert from string to store state | JSON.parse | | stringify | Converts stateful properties of your store to a JSON string | JSON.stringify |

Accessing the Store

Access and utilize the store in your components as follows:

import { gudamAuth } from '../store/authStore'

const Greeting = () => {
  const auth = gudamAuth()
  const loginAsDemo = () => {
    auth.setFirstName('Demo')
  }

  return (
    <>
      <div>{auth.greetingMessage}</div>
      {!auth.username && (
        <div>
          <button onClick={loginAsDemo}>Login as Demo User</button>
        </div>
      )}
    </>
  )
}
export default Greeting

Preloading Data

You can use the $preload built-in function to preload data into your store. It takes a callback function that receives an object with initial state values and returns the updated state. The following example provides additional details:

import { GudamContext, useGudam } from 'gudam'
import { gudamAuth } from '../store/authStore'

function App() {
  const gudam = useGudam()
  const auth = gudamAuth(gudam) // providing the gudam instance because the context is not provided yet
  auth.$preload((states) => {
    return { ...states, token: 'jwt-token', role: 'user' }
  })
  return (
    <GudamContext.Provider value={gudam}>
      {/* Your application is now empowered by Gudam */}
      <div>Your App Content</div>
    </GudamContext.Provider>
  )
}

export default App

Please note that once the store is changed, $preload does not execute its callback, and it does not support asynchronous operations.

Store Inside Store

Gudam follows a modular design, allowing you to use one store inside another. This can be achieved by creating an instance of the store:

export const gudamAccount = defineStore('account', {
  balance: 0,
  get auth() {
    return gudamAuth() // Create an instance of gudamAuth
  },
  doSomething() {
    this.auth.setFirstName('Hasinoor')
  },
})

Need Help?

If you require further assistance or have any questions, please don't hesitate to create an issue. The Gudam team is committed to simplifying your state management experience!