Socket
Socket
Sign inDemoInstall

@json_react_component/store

Package Overview
Dependencies
4
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @json_react_component/store

react的全局状态管理工具 react global state management tool


Version published
Maintainers
1
Install size
350 kB
Created

Readme

Source

JsonReactState


react的全局状态管理, 类似与redux, 功能简单, 可以直接使用异步actions, 不需要引用多于的库

React's global state management is similar to redux. It has simple functions and can use asynchronous actions directly without referencing more libraries.

用法(example):


// main.tsx
import {store} from "store/index.ts";
const root = createRoot(document.getElementById('app')!);
root.render(
  <React.StrictMode>
    <StoreProvider ctx={store.ctx} initState={store.initState} reducer={store.reducer}>
      {/* Component */}
    </StoreProvider>
  </React.StrictMode>
);

// store/index.ts
import { createStore } from "@json_react_component/store";
import demoStore from "./demo.ts";

export const store = createStore({
  demo: demoStore // 这个名称最好和slice的namespace相同
})

// store/connect.tsx
import { ComponentType } from 'react';
import JsonState from "@json_react_component/store/types";
import { connect as _connect } from "@json_react_component/store";
import { store } from "./index.ts";

export const connect = <T extends JsonState.ConnectProps<S> = any, S = any>(com: ComponentType<T>, filter: (state: any) => S) => _connect<T, S>(store.ctx, com, filter) as any;

// store/demo.ts
import { createSlice, createRequestDebounce } from "@json_react_component/store";
import {Dispatch} from "react";

const initState: any = {
  data: {}
}

function req(){
  return new Promise((resolve, reject)=> {
    setTimeout(()=> {
      resolve(1)
    }, 2000)
  })
}

// createSlice参数 namespace, initData
const demoSlice = createSlice<any>("demo", initState);

// 做请求防抖
const reqDebounce = createRequestDebounce(req);

const setDataReducer = demoSlice.addReducer("setData", (state: any, payload: any) => {
  state.data = payload;
  return state
})

export const reqAction = async (dispatch: Dispatch<any>) => {
  try {
    const res = await reqDebounce();
    dispatch({ type: setDataReducer.type, payload: res })
  } catch (error) {
    console.error(error)
  }
}

export default demoSlice

// 组件中使用state和actions
import {connect} from "@/store/connect";
import {reqAction} from "@/store/demo.ts";

function Demo({state, dispath}){
  useEffect(()=> {
    reqAction(dispatch);
  }, [])
  return <div>{state}</div>
}

export default connect(Demo, (state)=> state);


反馈(Contact us)

欢迎建议和反馈bug到邮箱 lijunsong2@gmail.com2622336659@qq.com Welcome to provide suggestions and report bugs via email at lijunsong2@gmail.com or 2622336659@qq.com.

Keywords

FAQs

Last updated on 15 Jan 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc