Skip to content

teod/react-behavior-state

Repository files navigation

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)

Argument Type Description
initialState object The initial state of the store represented by an object.
Returns:

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

createHook(store: Store)

Argument Type Description
store Store The store object returned by the createStore helper.
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.

About

context-less state management for react

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published