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

ninja-store

v1.2.4

Published

Create reusable state for your react components

Downloads

7

Readme

Ninja Store

Create reusable state for your react components.

npm i ninja-store -S

Step 1: Import the createStore function

import {createStore} from 'ninja-store';

Step 2: Create a store with unique name

const INITIAL_STATE = {};
const {useStore} = createStore('storeName', INITIAL_STATE);
// or 
const {useStore} = createStore('storeName', reducer, INITIAL_STATE);

Step 3: Use the store and export it with state management functions

// Refer to the examples below for more
function useComponent = function() {
    const [state, setState] = useStore();
    // or 
    const [state, dispatch] = useStore();

    return {
      state,
      // followed by methods managing the state
    };
}

Step 4: Import useComponent() in your components and start performing actions

function someOtherComponent() {
    const {state, actionFunction} = useComponent();
}

Example using State

import React from 'react';
import {View, Text, TouchableOpacity} from 'react-native';
import {createStore} from 'ninja-store';

const INITIAL_STATE = {
    count: 0
};

const {useStore} = createStore('counter', INITIAL_STATE);

function useCounter() {
    const [state, setState] = useStore();
    const increment = React.useCallback(() => {
        setState((state) => ({
            ...state,
            count: state.count + 1
        }));
    }, []);
    const decrement = React.useCallback(() => {
        setState((state) => ({
            ...state,
            count: state.count - 1
        }));
    }, []);
    return {
        state,
        increment,
        decrement
    };
}

function App() {
    return (
        <>
            <Counter />
            <Counter />
        </>
    );
}

function Counter() {
    const {state, increment, decrement} = useCounter();
    return (
        <View>
            <Text>Count: {state.count}</Text>
            <TouchableOpacity onPress={increment}>
                <Text>Increment</Text>
            </TouchableOpacity>
            <TouchableOpacity onPress={decrement}>
                <Text>Decrement</Text>
            </TouchableOpacity>
        </View>
    );
}

export default App;

Example using a Reducer

import React from 'react';
import {View, Text, TouchableOpacity} from 'react-native';
import {createStore} from 'ninja-store';

const INITIAL_STATE = {
    count: 0
};

function reducer(state, action) {
    switch(action.type) {
        case 'increment': {
            return {
                ...state,
                count: state.count + 1
            };
        }
        case 'decrement': {
            return {
                ...state,
                count: state.count - 1
            }
        }
        default: {
            return state;
        }
    }
}

const {useStore} = createStore('counter', reducer, INITIAL_STATE);

function useCounter() {
    const [state, dispatch] = useStore();
    return {
        state,
        dispatch
    };
}

function App() {
    return (
        <>
            <Counter />
            <Counter />
        </>
    );
}

function Counter() {
    const {state, dispatch} = useCounter();
    const increment = React.useCallback(() => {
        dispatch({ type: 'increment' });
    }, []);
    const decrement = React.useCallback(() => {
        dispatch({ type: 'decrement' });
    }, []);
    return (
        <View>
            <Text>Count: {state.count}</Text>
            <TouchableOpacity onPress={increment}>
                <Text>Increment</Text>
            </TouchableOpacity>
            <TouchableOpacity onPress={decrement}>
                <Text>Decrement</Text>
            </TouchableOpacity>
        </View>
    );
}

export default App;

Use mapState function to listen to only subset of store data


const INITIAL_STATE = {
    teachers: [],
    students: []
};

const {useStore} = createStore('users', INITIAL_STATE);

// Pass the mapState function to useStore function.
// This will be passed by the components which needs to listen only to the specific keys instead of the whole object
function useUsers(mapState) {
    const [state, setState] = useStore(mapState);
    return {
        state,
        // followed by other state management functions
    };
}

// This component will re-render only when there is a change in `teachers` array
function Teachers() {
    const [state, getTeachers] = useUsers((state) => ({ 
        teachers: state.teachers
    });
    //....
}

// This component will re-render only when there is a change in `students` array
function Students() {
    const [state, getStudents] = useUsers((state) => ({ 
        students: state.students
    });
    //....
}