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

react-behavior-state

v1.1.1

Published

context-less state management for react

Downloads

1

Readme

React Behavior State

npm version

Install | Why ? | Usage | Typescript | API | Examples

A simple solution for global state in a React application without using context. Under the hood it uses a custom BehaviorSubject pattern inspired from rxjs.

Install

npm:

npm install --save react-behavior-state

yarn:

yarn add react-behavior-state

Why ?

In most cases you should stick to the useState hook, this library is by no means a replacement for the component's internal state. This library can be used in instances where integrating context could be hard, impossible or there is no need to add context to your app. For instance:

  • Using react-native-navigation library with react-native.
  • You need a shared state in the two different parts of the application, and you don't see the need to wrap a big chunk of your tree into a context that will be used just by two components.
  • You don't want to add the boilerplate react's context comes with

This is a good overall replacement for big state libraries without any boilerplate and with proper typescript types.

This library is extremely tiny when properly packed into your application's bundle it shouldn't add a considerate amount of code to it.

Usage

stores/pets.js

import { createStore, createHook } from 'react-behavior-state'

const initialState = {
  kittens: ['Tigger', 'Tiger'],
  puppies: ['Max', 'Jake'],
}

const store = createStore(initialState)

export const usePets = () => createHook(store)

components/PetsList.js

import { usePets } from 'store/pets'

const PetsList = () => {
  // pets represents the current state of the store
  // setPets is the updater function that
  // sets new the state of the store
  const [pets, setPets] = usePets()

  return (...)
}

components/SetPets.js

import { usePets } from 'store/pets'

const PetsList = () => {
  const [, setPets] = usePets()

  const updateKittens = () => {
    // you can use a callback that will return
    // the current state of the store
    // in this case you have to extend the existing state
    setPets((currentPets) => ({
      ...currentPets,
      kittens: [...currentPets.kittens, 'Max', 'Smokey', 'Sam'],
    }))
    /*
      in => {
          kittens: ['Tigger', 'Tiger'],
          puppies: ['Max', 'Jake'],
      }

      out => {
        kittens: ['Tigger', 'Tiger', 'Max', 'Smokey', 'Sam'],
        puppies: ['Max', 'Jake'],
      }
    */
  }

  const updatePuppies = () => {
    // you can send the updated state directly as param
    // without a callback, in this case the existing
    // state will be extended from the one set as argument
    setPets({
      puppies: ['Buddy', 'Maggie', 'Bear'],
    })
    /*
      in => {
          kittens: ['Tigger', 'Tiger'],
          puppies: ['Max', 'Jake'],
      }

      out => {
        kittens: ['Tigger', 'Tiger'],
        puppies: ['Buddy', 'Maggie', 'Bear'],
      }
    */
  }

  return (...)
}

Typescript

React Behavior State is written in typescript and all of the types are exported through the node module.

By default the state interface will be infered from the object provided as initial state. You can provide your own interface to the createStore helper:

interface State = {
  firstName: string
  lastName: string
}

const store = createStore<State>({ firstName: '', lastName: '' })

API

createStore(initialState: object)

Returns:

The store object that can be passed to the createHook helper function for creating hooks for a specific store.

createHook(store: Store)

Returns:

A react hook which returns an array with the state and the setState update function, the same exact api used by the react's useState hook.

Run the example

In order to run the example you need to clone the github repository:

cd examples/basic
yarn
yarn start

This will open a new webpack server on port 3000 by default.