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

use-custom-store

v0.1.0

Published

一个只用react hook api实现的全局和局部状态管理机

Downloads

1

Readme

use-hooks-store

This is a state manager implemented only with the react hooks API<useContext,useReducer>

use createHookStore, you can create different state managers, global or local

be careful:Used this tool, you have to accept the use of react hooks to develop your project

这是一个仅用react hooks api<useContext,useReducer>实现的状态管理器

通过createHookStore,你可以创建不同的状态管理器,既可以是全局的,也可以是局部的

注意:使用这个工具,你必须接受使用react hooks来开发你的项目

installation

To install the stable version:

npm install --save-dev use-hooks-store

That's it!

import createHookStore from 'use-hooks-store';
const reducers = {reducer1,reducer2,reducer3,...};
const { useStore, useProvider, getAsyncStore } = createHookStore(reducers);
//To avoid naming conflicts, alias
export const useGloblaStore = useStore;
export const useGloblaProvider = useProvider;
export const globlaAsyncStore = getAsyncStore;
//useStore,Use within components
function App(){
    const [state,dispatch] = useGloblaStore();
    return <div>{state}</div>;
}
//getAsyncStore,For external use, such as in encapsulated request functions or other utility functions
function asyncRequest(){
    const [state,dispatch] = globlaAsyncStore();
    setimeout(() => {
        dispatch({type:'xxx',data: xxx});
    },2000);
}

Example

project.js

const initState = {
    name: '金融城项目',
    desc: '关于金融城项目的描述'
};
export default function (state = initState, action) {
    switch (action.type) {
        case 'init_project':
            state.name = action.data.name;
            state.desc = action.data.desc;
            break;
    }
    return Object.assign({}, state);
}

user.js

const initState = {
    name: '',
    role: ''
};
export default function (state = initState, action) {
    switch (action.type) {
        case 'init_user':
            state.name = action.data.name;
            state.role = action.data.role;
            break;
    }
    return Object.assign({}, state);
}

store.js

import project from './project';
import user from './user';

const reducers = { user, project };
const { useStore, useProvider, getAsyncStore } = createHookStore(reducers);
//To avoid naming conflicts, alias
export const useGloblaStore = useStore;
export const useGloblaProvider = useProvider;
export const globlaAsyncStore = getAsyncStore;
//app.js
import {useGloblaStore,useGloblaProvider,globlaAsyncStore} from './store';
import {getUserDataApi} from './api';
function asyncRequest(params){
    //anync request
    const [state, dispatch] = globlaAsyncStore();
    getUserDataApi(params).then((data) => {
        const action = {type: 'init_user',data: data};
        dispatch(action);
    });
}
function View(){
    const [state, dispatch] = useGloblaStore();
    //or
    const [, dispatch] = useGloblaStore();
    //or
    const [state] = useGloblaStore();
    //async
    useEffect(() => {
        asyncRequest(1);
    },[]);
    return <div>{state}</div>; 
}
function App(){
    //useStore() cannot be used within a top-level component,
    //At this time, the Provider has not injected the store into the View
    return useGloblaProvider(View);
}
ReactDOM.render(
    <App />,document.body
);