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-reactive-context

v1.1.8

Published

extending react context functionalities

Downloads

31

Readme

react-reactive-context

NPM JavaScript Style Guide

Table of Contents

About

The purpose of this module is increase the functionality of React.Context without add into the project a big library like redux. This module work in the same way of base React.Context but adds the possibility to set a new value without update Context.Provider prop, also allows to read Context state where you want into the code, there is also the possibility to add subscribers called that will be called when an update of Context occurs. Finally, the creator function or Context.Provider component accept a decorator that receives a current Context state and a set of attributes from each reader (hook, Consumer, subscriber) to customize the state for each of them.

Installing

npm install --save react-reactive-context

Doc

createReactiveContext

return a new Context, the function accepts two parameters

| name | type | description | | --------- | ---- | --------------------------------------------------------------------------------- | | state | any | default context value | | decorator | func | when the state change take its value and return a new decorated state |

Context.Provider

Provider accepts the following props @seeProvider | name | type | description | | --------- | ---- | ------------------------------------------------------------------------------------------ | | value | any | context value | | decorator | func | when the state change take its value and return a new decorated state |

Context.Consumer

Consumer props are passed like decorators to decorator function @see Consumer

useReactiveContext

trigger component update if something changes, accepts two parameters | name | type | description | | ---------- | ---------------- | --------------------------------------------------------------------------------- | | Context | ReactiveContext | Context variable | | decorators | object | passed to decorator function |

subscribe

accepts two parameters | name | type | description | | ---------- | ---------------- | --------------------------------------------------------------------------------- | | callback | func | return current decorated state | | decorators | object | passed to decorator function |

set

update a Context, accepts new state value

get

return current Context status everywhere, accepts a decorator object

removeAllSubscribers

remove all subscribtion registered by subscribe method

Usage

Import

    import React, { useEffect, useState } from "react";
    import { createReactiveContext, useReactiveContext } from "react-reactive-context";

Initialization

    type MyContext = {
        color: string,
        supportColor?: string
    };

    type MyContextDecorated = {
        ...
    };

    type MyDecorators = {};

    //Context initialization, could be empty
    const Context = createReactiveContext<MyContext, MyContextDecorated, MyDecorators>({
        supportColor: "#AA0000"
    }, (state) => processedState);

    <Context.Provider value={state} decorator={(state) => processedState}>
        {children}
    </Context.Provider>

Hook

    //update component by custom hook
    const {state, decoratedState} = useReactiveContext(Context, { /* decorators returned inside decorator function */ });

Consumer

    <Context.Consumer /* all props except children are passed like decorators to decorator */>
        {({state, decoratedState}) => null}
    </Context.Consumer>

Subscriber

   useEffect(() => {
        //listen to any Context change
        const subscription = Context.subscribe(({state, decoratedState}) => {
            
        }, {/* decorators returned inside decorator function */});

        return () => {
            //unsubscribe
            subscription();
        };
    }, []);

Setter

    Context.set(state);

or you could update Context.Provider value

Getter

    const {state, decoratedState} = Context.get(/* decorators returned inside decorator function */);

License

MIT © andmau90