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-scomponent

v2.0.3

Published

Barebones esbuild and test node server implementation. For building

Downloads

10

Readme

sComponent

status downloads size lic

npm i react-scomponent

Mixes anotherstatemanager functionality into React by wrapping the component's setState function in React.Component with the ability to setState on a shared global state object.

The benefits of this are:

  • Components don't require writing any extra logic to have cascading effects on components across your app, or to talk to external scripts subscribed to your shared state object. Just setState in the component as you would normally!
  • Makes a lot of react quality of life tools (e.g. routers, redux store) sort of irrelevant, write a whole app with just components and some state subscription logic.
  • Easy to cache the global state as JSON to save state on your components.

You can create new state objects and pass them as props otherwise a default state can be imported. The component always updates its render first then sets state on the shared object.

The state object will check on frame for updates to its own values or can be triggered immediately with state.setState({etc:'etc..'}).

If you update a state value on your component, it will update the corresponding state value on the shared state object so it will be propagated automatically to other components or wherever else in your script.

Just extend the sComponent as you would a normal react component!

import {state, sComponent} from 'react-scomponent'
class myComponent extends sComponent {
    state = {
        clicked:false
    }
    
    onclicked() {
        this.setState({clicked:!this.state.clicked})
        
        console.log(state.data)
    }

    render() { 
        (
            <div> 
                <button onClick={this.onclicked}>Clickme</button>
                {this.state.clicked && 
                    <div> Clicked on! </div>
                }
            </div>
            
        )
    }
}

state.subscribeTrigger('clicked',(res)=>{
    console.log('clicked!');
})

Just make sure you preserve the state prop if you need to add to those. Else it defaults to the provided state object.

The component code:


import { Component } from 'react'
import {StateManager} from 'anotherstatemanager'

export const state = new StateManager({ }); //globally available state object

//These components share their state with the global state, and changes propagate both directions with setState
export class sComponent extends Component {

    statemgr=state
    UPDATED=[]

    constructor(
        props={
            state:state //can apply a new state other than the global state so you can have states for certain pages for example
        }
    ) {
        super(props);

        if(props.state)
            this.statemgr = props.state;

        //lets overload setState
        let react_setState = this.setState.bind(this);
        
        this.setState = (s={}) => {

            this.UPDATED = Object.keys(s);
            react_setState(s);
            if(typeof s === 'object') {            
               state.setState(s); //now relay through statemanager
            }
        }

        //so this runs AFTER the inherited constructor
        setTimeout(()=>{
            let found = {};
            for(const prop in this.state) { //for all props in state, subscribe to changes in the global state
                this.statemgr.subscribeTrigger(prop,(res)=>{
                    let c = this;
                    if(typeof c === 'undefined'){
                        this.statemgr.unsubscribeTrigger(prop);
                    }
                    else {
                        let wasupdated = this.UPDATED.indexOf(prop);
                        if( wasupdated > -1) {
                            this.UPDATED.splice(wasupdated,1);
                        }
                        else {
                             react_setState({[prop]:res});//only updates one prop at a time rn
                        }
                    }
                });
            }
            if(Object.keys(found).length > 0) react_setState(found); //override defaults
        },0.001);
        
    }

}

StateManager example code:


import {StateManager} from 'anotherstatemanager'

let state = new StateManager(
    {x:1}
    "FRAMERATE" //or 1000 (ms) etc.
    true //start the subscription loops automatically? False to just use the trigger state
    );

let sub = state.subscribe('x',(newx) => {console.log(newx);});


let sub = state.subscribe(key,onchange); //triggers changes on frame
state.unsubsribe(sub); //pass the key you received from .subscribe to remove the function
state.subscribeOnce(key,onchange);

let subt = state.subscribeTrigger(key,onchange); //fire the function when you setState, these run independent of the interval based functions so you can have on-demand functions and on-frame/interval functions
state.unsubscribeTrigger(subt); //pass the key you received from .subscribe to remove the function
state.subscribeTriggerOnce(key,onchange);

let subs = state.subsribeSequential(key,onchange); //this is a sequence state manager so it fires on the update interval and for each update pushed between the update periods e.g. tallying key inputs. This won't relate to the state component by default.
state.unsubscribeSequential(sub);
state.subscribeSequentialOnce(key,onchange);
//...

state.setState({x:3});
state.setState({...keys:values});