项目作者: bplok20010

项目描述 :
https://bplok20010.github.io/react-fax-store
高级语言: TypeScript
项目地址: git://github.com/bplok20010/react-fax-store.git
创建时间: 2020-03-20T06:51:13Z
项目社区:https://github.com/bplok20010/react-fax-store

开源协议:MIT License

下载


react-fax-store

基于React16的Context简单封装的状态管理库

安装

npm install --save react-fax-store

使用

  1. import {createStore} from 'react-fax-store';
  2. const Store = createStore(() => ({text: 'fax-store'}));
  3. function App(){
  4. const data = React.useContext(Store.Context); // {text: 'fax-store'} subscribe state
  5. const state = store.useState(); // {text: 'fax-store'} subscribe state
  6. const text = store.useSelector( state => state.text ); // fax-store subscribe only state.text change
  7. const store = store.useStore();
  8. store.getState(); // {text: 'fax-store'}
  9. store.setState({
  10. text : 'react-fax-store'
  11. });
  12. // or
  13. const update = store.useUpdate();
  14. update({
  15. text : 'react-fax-store'
  16. });
  17. return <Store.Consumer>{ state => console.log(state) }</Store.Consumer>
  18. }
  19. <Store.Provider>
  20. <App ></App>
  21. </Store.Provider>

createStore(initialValue: () => {}): Store;

创建Store对象

  1. import {createStore} from 'react-fax-store';
  2. createStore(() => {
  3. return {
  4. ...
  5. }
  6. })

createReducer((prevState: {}, action: {type:string, [x:string]: any}),initialValue: () => {}): ReducerStore

创建Store对象

  1. import {createStore} from 'react-fax-store';
  2. createStore(() => {
  3. return {
  4. ...
  5. }
  6. })

Store

Provider

  1. <Store.Provider initialValue={{name: 'react-fax-store'}}>
  2. ...
  3. </Store.Provider>

Consumer

  1. <Store.Provider>
  2. ...
  3. <Store.Consumer>
  4. {state => {
  5. return <div>{state}</div>
  6. }}
  7. </Store.Consumer>
  8. ...
  9. </Store.Provider>

useState

订阅整个数据

  1. function Info(){
  2. const state = Store.useState();
  3. return <div>{state}</div>
  4. }

useSelector

订阅指定数据

  1. function Info(){
  2. const state = Store.useSelector(state => {
  3. return {
  4. username: state.username
  5. }
  6. });
  7. return <div>{state.username}</div>
  8. }

useUpdate

更新数据

  1. function Action(){
  2. const update = Store.useUpdate(prevState => {
  3. return {
  4. username: prevState.username + '_xc'
  5. }
  6. });
  7. return <button onClick={update}>Add</button>
  8. }

useProvider

别名:useStore

获取由Provider提供的store数据对象

  1. const store = Store.useStore();
  2. store.getState();
  3. // or
  4. store.setState(...)

Context

可直接通过React.useContext获取数据

  1. const state = React.useContext(Store.Context);

ReducerStore

继承Store

useDispatch

  1. const dispatch = React.useDispatch();
  2. dispatch({
  3. type: 'ADD',
  4. data: 1,
  5. })

interface

  1. export declare type Update<T = {}> = <K extends keyof T>(state: ((prevState: Readonly<T>) => Pick<T, K> | T | null) | Pick<T, K> | T | null) => void;
  2. export declare type Subscriber<T = {}> = (prevState: Readonly<T>, nextState: Readonly<T>) => void;
  3. export declare type UseSelector<T = {}> = <S extends (state: T) => any>(selector: S) => ReturnType<S>;
  4. export declare type UseUpdate<T = {}> = () => Update<T>;
  5. export declare type UseState<T = {}> = () => T;
  6. export declare type UseProvider<T> = () => Provider<T>;
  7. export declare type Consumer<T = {}> = React.FC<ConsumerProps<T>>;
  8. export declare type Context<T> = React.Context<T>;
  9. export declare type ReducerAction = {
  10. type: string;
  11. [x: string]: any;
  12. };
  13. export declare type Reducer<T> = (state: T, action: ReducerAction) => T;
  14. export declare type Dispatch = (action: ReducerAction) => void;
  15. export declare type UseDispatch = () => Dispatch;
  16. export interface ConsumerProps<T = {}> {
  17. children: (state: T) => React.ReactElement | null;
  18. }
  19. interface ProviderProps<T> {
  20. initialValue?: T;
  21. }
  22. export interface Provider<T = {}> extends React.Component<ProviderProps<T>, T> {
  23. __$isProvider: boolean;
  24. getSubscribeCount(): number;
  25. subscribe(subscriber: Subscriber<T>): () => void;
  26. getState(): T;
  27. }
  28. export interface Store<T = {}> {
  29. Context: Context<T>;
  30. Provider: new (props: {}) => Provider<T>;
  31. Consumer: Consumer<T>;
  32. useProvider: UseProvider<T>;
  33. useStore: UseProvider<T>;
  34. useState: () => T;
  35. useSelector: UseSelector<T>;
  36. useUpdate: UseUpdate<T>;
  37. }
  38. export interface ReducerStore<T = {}> extends Store<T> {
  39. useDispatch: UseDispatch;
  40. }
  41. export declare const withHooks: <T extends typeof React.Component>(component: T) => T;
  42. export declare function createStore<T extends Record<string | number | symbol, any>>(initialValue: () => T): Store<T>;
  43. export declare function createReducer<T>(reducer: Reducer<T>, initialValue: () => T): ReducerStore<T>;

Example

store.js

  1. import {createStore} from 'react-fax-store';
  2. export default createStore(() => {
  3. return {
  4. name: 'react-fax-store';
  5. }
  6. });

index.js

  1. import React from 'react'
  2. import Store from './store';
  3. function Info(){
  4. const state = Store.useState();
  5. return <div>{state.name}</div>
  6. }
  7. function App(){
  8. return (
  9. <Store.Provider initialValue={{name: 'test'}}>
  10. <Info ></Info>
  11. </Store.Provider>
  12. );
  13. }
  14. export default App;