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

rx-hook-store

v1.0.3

Published

this module provides a global store management for react 16+, depends on RxJs.

Downloads

11

Readme

React Store with rxjs & hooks

this module provides a global store management for react 16+, depends on RxJs.

also provide two useful hooks useSubscriptionStack and useManualSubscription to manage rx subscriptions within function components.

Installation

npm i rx-hook-store

Usage

create a store

import StoreClass from 'rx-hook-store'

interface NewStoreState {
    [random: string]: any;
}

const initialState: NewStoreState = {
    anystuff: 'anystuff'
}

class NewStore extends StoreClass<NewStoreState> {
    constructor() {
        super(initialState)
    }
}

const store = {
    newStore: new NewStore()
}

connect to store inside a function component

import {useStore} from 'rx-hook-store'

function NewComponent () {
    const newStore = useStore<NewStoreState>(store.newStore)

    return <>{newStore.anystuff}</>
}

Demonstration

clone this repository git clone https://github.com/beiduo/rx-store.git install, and run npm start

Examples

see /example/src directory

Concepts

the concept of "store" is inspired by the service of angular. to create a "service" (in here, it's a new member of the store), you need to extend the StoreClass to get a new class, set the initial value, and manage all the value operation methods in this class. then, create an instance of this new class, this instance can be used not only in components, also in basically everywhere, by different ways.

although, it's highly recommended to follow the pattern inside example/src/store directory.

StoreClass

StoreClass provides the fundamental behavior for each store instance.

  • update(payload: any): merge new value into the store
  • replace(payload: any): replace the value of the store
  • reset(): reset the store to the initial value
  • getValue(): get the current value of the store

Store Instance

a store instance provides a value, and a variety of methods to read and operate that value.

Hooks

useStore

this hook connects a store with a function component, and return the current value of that store.

useSubscriptionStack

this hook returns a function, this function can receive a subscription as its parameter, this subscrition can be automatically cancelled.

the most common usage is to automatically cancel subscriptions (such as ajax request, setTimeout, etc) while leaving the interface.

example:

import {useSubscriptionStack} from 'rx-hook-store';

const ExampleComponent = () => {
    const newStore = useStore<NewStoreState>(store.newStore)

    const addSubscription = useSubscriptionStack();

    const refresh = () => {
        addSubscription(store.newStore.fetch());
    };

    return (
        <>
        </>
    )
}

useManualSubscription

this hook returns an object with two methods, "start" and "cancel", "start" can receive a subscription as its parameter, if repeatedly call this method, it will cancel previous subscription, then process the new subscribe. "cancel" will just cancel the subscription.

it might be useful in complicated scenes.

example:

import {useManualSubscription} from 'rx-hook-store';

const ExampleComponent = () => {
    const newStore = useStore<NewStoreState>(store.newStore)

    const handleFetch = useManualSubscription();

    const refresh = () => {
        handleFetch.start(store.newStore.fetch());
    };

    useEffect(() => {
        return () => {
            handleFetch.cancel();
        }
    }, []);

    return (
        <>
        </>
    )
}