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 🙏

© 2025 – Pkg Stats / Ryan Hefner

react-tivity

v1.0.1

Published

State solution for React

Downloads

17

Readme

React-Tivity

State solution for React

Installation

npm

npm i react-tivity

yarn

yarn add react-tivity

Table of Contents

Introduction

Easy and Small state management library for React with hooks based api and zero configuration. With react-tivity you don't need to pass the selector in the hook you get the whole state that you can destructure and your components will get updated only when the value they are consuming is changed.

  • Zero boilerplate.
  • Zero configuration
  • Small & Easy.
  • Hooks based Api.
  • Typescript Support.

create

create takes an object or initializer and returns a hook to be used in components.

Example on StackBlitz

import { create } from 'react-tivity'

Then initialize store by passing object or initializer

const useCount = create({
  count: 0,
  inc: state => state.count++,
  dec: state => state.count--
})

Usage in react component

function Counter() {
  let { count, inc, dec } = useCount()
  
  return (
    <div>
      <h1>{count}</h1>
      <button onClick={inc}>Count++</button>
      <button onClick={dec}>Count--</button>
    </div>
  )
}

Some apis are assigned to the hook and can be used in or outside of react component.

// you can get access to state object
let count = useCount.state
// calling methods
count.inc()
count.dec()
// reading values
count.count
// or setting values
count.count++


// subscribe
let callback = () => console.log('count changed')
let unsubscribe = useCount.subscribe(callback)  // will log 'count changed' every time state value changes

// to unsubscribe call the variable you assigned subscription to
unsubscribe()

reduce

reduce takes a reducer function as first argument and object or initializer which returns an object as second argument. You can pass your state object without any method and retrieve dispatch function assigned to hook itself as method.

Example on StackBlitz

import { reduce } from 'react-tivity'

Then initialize store by passing reducer & object or initializer as second.

function reducer(state, action) {
  switch(action.type) {
    case 'inc':
      return {
        count: state.count + 1
      }
    case 'dec':
      return {
        count: state.count - 1
      }
  }
  throw Error('unknown action type')
}

const useCount = reduce(reducer, {
  count: 0
})

Usage in react component.

function Counter() {
  let { count, dispatch: countDispatch } = useCount()
  
  return (
    <div>
      <h1>{count}</h1>
      <button onClick={() => countDispatch({type: 'inc'})}>Count++</button>
      <button onClick={() => countDispatch({type: 'dec'})}>Count--</button>
    </div>
  )
}

Some apis are assigned to the hook and can be used in or outside of react component.

// all apis from create such as subscribe and state object are assigned

// dispatch
let dispatch = useCount.state.dispatch
dispatch({ type: 'inc'})
dispatch({ type: 'dec'})

persist

persist works same as create if only one argument is passed if passed two arguments first reducer and second object it acts as reduce. It takes an additional property config which won't be saved as a state value. It will persist your state object in either storage created by itself or the custom storage you provide. It accepts asynchronous storage only, but for convenience you can pass 'local' or 'session' to create asynhronous localStorage & asyncronous sessionStorage respectively.

Example on Stackblitz

import { persist } from 'react-tivity'

Then initialize store by passing object or initializer

// acts as create
const useCount = persist({
  count: 0,
  inc: state => state.count++,
  dec: state => state.count--,
  config: {
    key: '@count' // required,
    storage: 'session' // defaults to 'local'
  }
})

// acts as reduce
const useCount = persist(reducer, {
  count: 0,
  config: {
    key: '@count',
    storage: 'session' // defaults to 'local'
  }
})

config property

const useStore = persist({
  // First argument reducer you want it to act as `reduce` and then `object` or `initializer`
  // ...
  config: {
    // Only required property of config
    key: 'string',
    // Any asynchronous storage which has setItem, getItem and removeItem properties, defaults to 'local' can also accept 'session'
    storage: 'local' | 'session' | AsyncStorage,
    // To serialize the data to be saved in chosen storage, defaults to JSON.stringify()
    serialize: (state) => JSON.stringify(state),
    // To deserialize saved data when retrieved from chosen storage, defaults to JSON.parse()
    deserialize: (state) => JSON.parse(state),
    // An array of state slices not to save for eg. ['count'], defaults to []
    blacklist: [],
    // Required if you change your structure of your state otherwise optional, defaults to 0
    version: 0,
    // Required if you have changed version, So you can migrate your previously saved state values to current one. defaults as below.
    migrate: (current, previous) => current
  }
})

// More on migrate, In order to migrate between version change
// You receive Current State & Previous State so you can decide what to keep what to throw, eg. below

const migrate = (curr, prev) => {
  if(prev.version === 0) {
    current.upvotes = prev.likes    // Current state's `upvotes` slice will get hydrated with value previous state's `likes`
    return current                  // Return current now
  }
}

Internal _status slice

Asynchronous storages will hydrate stores asynchronously it means that user can have a flash of initial state before store gets hydrated and the view gets updated. To overcome this problem a _status slice is managed internally. The value of _status is false initially and when asynchronous task gets done it is set to true so you can wrap your child components consuming that state in a parent wrapper component to prevent flash of that initial state by rendering a loader component until store gets hydrated.

// Note: `_status` property gets set to true even if there was no state saved in the storage.
function PersistWrapper() {
  let { _status } = useCount()
  
  if(!_status) return <h1>Loading...</h1>
  
  return <ChildComponent />
}

function ChildComponent() {
  // child component consuming useCount's state
}

Some apis are assigned to the hook and can be used in or outside of react component.

// all apis from `create` and `reduce`

// persist
let persist = useCount.persist // or just useCount.persist.clearStorage()
persist.clearStorage()    // clears the storage assigned to useCount

Typescript

create

type State = {
  count: number;
  inc: (state: State) => void;
  dec: (state: State) => void;
}

const useCount = create<State>({
  count: number,
  inc: (state) => state.count++,
  dec: (state) => state.count--
})

reduce

type State = {
  count: number;
}

type Action = {
  type: string;
}

function reducer(state: State, action: Action) {
  //....
}

const useCount = reduce<State, Action>(reducer, {
  count: 0,
})

persist

If using as create same as create just add config.

type State = {
  count: number;
  inc: (state: State) => void;
  dec: (state: State) => void;
  config: {
    key: string;
    storage: string;
  }
}

const useCount = create<State>({
  count: number,
  inc: (state) => state.count++,
  dec: (state) => state.count--,
  config: {
    key: '@count',
    storage: 'session',
  }
})

If using as reduce.

type State = {
  count: number;
  config: {
    key: string;
    storage: string;
  }
}

type Action = {
  type: string;
}

function reducer(state: Omit<State, 'config'>, action: Action) {
  //....
}

// Pass second generic argument as true if you are using persist as reduce
const useCount = reduce<State, true, Action>(reducer, {
  count: 0,
  config: {
    key: '@count',
    storage: 'session'
  }
})

License

Licensed under MIT License