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

use-global-hook-ts

v1.1.2

Published

State management for React using Hooks with Typescript. No dependencies.

Downloads

8

Readme

use-global-hook-ts

State management for React using Hooks with Typescript.

Requirements

This library needs React and ReactDOM as peer dependencies with a version greater than 16.8 because Hooks have been introduced in that version.

Installation

npm i use-global-hook-ts

Docs

The method useGlobalHook accept four paramters:

  1. React: Your instance of React;
  2. initialState: the initial state of your app;
  3. options: store configurations:
    • persistTree: a subset of app state's keys with boolean values that will be saved and auto refilled on page reload OR true if you want to save all the state OR false if you want to save nothing;
    • persistExp: you can pass the number of seconds after which the stored state will be discarded;
    • debug: if true each action that change the state of app will be logged in console;
    • undoable: if true you will enable the undo/redo features;
    • maxUndoable: you can pass here the max number of undo/redo history that you want to use;
    • More options will come soon!
import createStore, { IStore } from 'use-global-hook-ts'

interface IAppState {
  movies: string[],
  loggedUser: any
}

const initialState: IAppState = {
  movies: [],
  loggedUser: null
}

const { useGlobal, store } = createStore(React, initialState, {
  debug: true,
  persistTree: {
    movies: false,
    loggedUser: true
  },
  persistExp: 60,
  undoable: true,
  maxUndoable: 10
})

const actions = {
  movies: {
    addMovie: (movie: string) => {
      const newMovies = [ ...store.state.movies, movie ]
      const newState = { ...store.state, movies: newMovies }
      store.setState(newState)
    }
  }
}

Inside each action you have to use the method setState of store in order to change the app state and you can also read the latter using the property state of the store.

When call useGlobal inside a component you can pass an object with a sub set of app state's keys with boolean values. The component will be updated only when the sub set of app state will change (see Example).

useGlobal returns two elements when is called inside a component:

  1. globalState: last version of the app state;
  2. lastChanges: last changes made on the app state (null if no changes are made yet).

Immutability

With the option freezable the app state is immutable (by a deep freeze function) in order to guarantee that any component can't change it without using the actions. If you try to directly mutate the state in strict mode, it will throw an error, otherwise it will fail silently.

Persist

With the option persistTree you can achieve almost all of what you can do with redux-persist blacklist and whitelist. If you want to save only some keys of your app state, just set them with true inside the persistTree. In a similar manner, if you want to exclude some other keys, set them to false. For example, if your app state is:

const appState = {
  someData: [1, 2, 3],
  tmpData: 'Tmp'
}

If you want to save only someData you can use a persistTree like:

const persistTree = {
  someData: true
}

Class component

You can use this package also with class components wrapping them into a HOC Component (see Example).

Undo/Redo

The createStore returns the property historyActions with the methods undo and redo that you can use respectively in order to revert or reapply the last action. You have to pass undoable: true in the options of the store in order to use this feature.

This feature will be improved in the next releases.

React Native

You can use this library also for React Native development but, for the moment, you will have to set persistTree option to false.

Basic example

import React from 'react'
import createStore, { IStore, store } from 'use-global-hook-ts'

interface IAppState {
  text: string,
  data: string
}

const initialState: IAppState = {
  text: 'Hello',
  data: 'Useless'
}

const { useGlobal, store } = createStore(React, initialState, {
  debug: true
})

const actions = {
  changeText: (newText: string) => {
    store.setState({ text: newText })
  }
}

const ExampleComponent = (_: any) => {
  const [globalState, lastChanges] = useGlobal()
  React.useEffect(() => {
    setTimeout(() => {
      actions.changeText('New text')
    }, 1000)
  }, [])
  console.log('ExampleComponent render. Last changes', lastChanges)
  return <span>{globalState.text}</span>
}

const AnotherComponent = (_: any) => {
  const [globalState] = useGlobal({
    data: true
  })
  console.log('AnotherComponent render')
  return <span>{globalState.text}</span>
}

class ClassComponent extends React.Component<{ text: string }> {
  render() {
    return <span>{this.props.text}</span>
  }
}

const HOCWithGlobalHook = (_: any) => {
  const [globalState] = useGlobal({
    text: true
  })
  console.log('HOCWithGlobalHook render')
  return <ClassComponent text={globalState.text} />
}
const Wrapper = (_: any) => {
  return <>
    <ExampleComponent/>
    <AnotherComponent/>
    <HOCWithGlobalHook/>
  </>
}

For any question or request, feel free to open an issue on Github!

TODO

  • Improve Docs;
  • Add more tests;
  • Improve management of undo/redo;

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)