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

stateux

v0.1.3

Published

Really simple Redux like lib without reducers for state control

Downloads

18

Readme

npm package

NPM version NPM License NPM Downloads

Support me for future versions:

BMC

PAGSEGURO

stateux

Really simple Redux like lib without reducers for state control


/*
    Example using createStore utility
    ** state field its used as initial state 
    ** all functions are binded to the store object
    ** you can also use Store.create
*/
import { createStore } from 'stateux';

export default createStore({
    counter: 0,

    //initialState
    state: {
        items: []
    },

    add(text){
        this.state.items.push({ id: ++this.counter, text });

        //the second parameter its the action name
        //its optional but can be usefull to subscribe ONLY to this action update if you want
        this.dispatch(this.state, 'add');   
        //this.dispatch function returns the state passed as parameter
        //so you can use a simplified dispatch and return
        //return this.dispatch(this.state);     
    },

    remove(id){
        this.state.items = this.state.items.filter((item)=> item.id != id);

        this.dispatch(this.state, 'remove');
    }
});

/*
    Example using useStore utility
*/
import { setState } from 'react';
import { useStore } from 'stateux';

function TodoList(){


    const todoList = useStore(TodoListStore);
    //you can also subscribe only to an action like add or remove
    //const todoList = useStore(TodoListStore, 'add');

    const [state, setState] = useState({text: ''});

    return (
            <div>
                {todoList.items.map((item)=> {
                    return (
                    <div key={item.id}
                          onClick={()=> TodoListStore.remove(item.id)}>
                          
                            {item.text}
                    </div>)
                })}

                <input type="text" defaultValue="" onChange={(e)=> setState({ text : e.target.value })}/>
                <button onClick={()=> TodoListStore.add(state.text)}>
                    Add
                </button>
            </div>
        )
    }
}

/*
    Example using subscribe and unsubscribe + useEffect utility
*/
import { setState } from 'react';

function TodoList(){
    
    //you can also subscribe only to an action like add or remove
    //const todoList = useStore(TodoListStore, 'add');

    const [state, setState] = useState({text: ''});

    //more about useEffect: https://reactjs.org/docs/hooks-reference.html#cleaning-up-an-effect
    useEffect(()=> {
        //if its a initialState, some previous state set or a update the callback will be called
        //subscribe function returns the unsubscribe function
        return TodoListStore.subscribe((storeState, action)=>{
            //action name will be null if its the initialState
            //you can check the action name if you want ex: action === 'add' || action === null
            //merge the states or replace if you want
            setState({ ...state, ...storeState}); 
        });

        //you can subscribe only to an action updates if you want 
        //const unsubscribe = TodoListStore.subscribe(callback, actionName)
        
    }, [TodoListStore])

    return (
            <div>
                {todoList.items.map((item)=> {
                    return (
                    <div key={item.id}
                          onClick={()=> TodoListStore.remove(item.id)}>
                          
                            {item.text}
                    </div>)
                })}

                <input type="text" defaultValue="" onChange={(e)=> setState({ text : e.target.value })}/>
                <button onClick={()=> TodoListStore.add(state.text)}>
                    Add
                </button>
            </div>
        )
    }
}

/*

 Using class to create a Store
 
 */ 

import Store from 'stateux';
//or
//import { Store } from 'stateux';

class TodoListStore extends Store{

    constructor(){

        this.counter = 0;
        this.state = {
            items: []
        };
        //set as initial state
        this.initial(this.state);

        /*
        //you can set directly too and use this.getState() to get the state object
        this.initial({
            items: []
        });
        let state = this.getState();
        */
    }

    add(text){
        this.state.items.push({ id: ++this.counter, text });

        //the second parameter its the action name
        //its optional but can be usefull to subscribe ONLY to this action update if you want
        this.dispatch(this.state, 'add');   
        //this.dispatch function returns the state passed as parameter
        //so you can use a simplified dispatch and return
        //return this.dispatch(this.state);   ;
    }

    remove(id){
        this.state.items = this.state.items.filter((item)=> item.id != id);

        this.dispatch(this.state, 'remove');    
    }
}

export default new TodoListStore();

/*
 Using class components
*/


import TodoListStore from './TodoListStore';

class TodoList extends React.Component{
    
    constructor(props, context){
        super(props, context);

        //TodoListStore.subscribe returns the unsubscribe function
        this.unsubscribeTodoList = TodoListStore.subscribe((state) => this.setState(state));
        /*
        //you can filter actions if you need
        //action parameter will be null if its the initial state

        this.unsubscribeTodoList = TodoListStore.subscribe((state, action) => {
            if(action === "add" || action === null){
                this.setState(state);
            }
        });
        
        //or you can subscribe passing a action name too
        //in this case will be called only if the action with this name is dispatched or if its the initial state

        this.unsubscribeTodoList = TodoListStore.subscribe((state) => this.setState(state), "add");

        */
    }

    componentWillUnmount(){
        this.unsubscribeTodoList();
    }

    updateText(e){
        this.setState({ text : e.target.value });
    }
        
    render(){
        
        return (
            <div>
                {this.state.items.map((item)=> {
                    return (
                    <div key={item.id}
                          onClick={()=> TodoListStore.remove(item.id)}>
                          
                            {item.text}
                    </div>)
                })}

                <input type="text" defaultValue="" onChange={(e)=> this.updateText(e)}/>
                <button onClick={()=> TodoListStore.add(this.state.text)}>
                    Add
                </button>
            </div>
        )
    }
}

export default TodoList;