1.0.68 • Published 2 years ago

an-store v1.0.68

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

AnStore

一款恰到好处的全局状态管理库

安装

npm install an-store
or
yarn add an-store

快速上手

初始化

export store = new Store({
    state: { // 初始化数据
        name: 'jack',
        age: 36
    },
    site: 'local',
    rootKey: 'myStore'
})

获取状态

store.getState('name')
// => jack

修改状态

store.dispatch('name', 'tom')

重置状态

store.init({name: 'rose', age: 24})

删除状态

store.remove('name')

API

constructor

初始化 store

  • state ? Object // 可选 初始化的数据
  • site ? string // 可选 缓存的位置,当运行环境为浏览器时,可选值为 'local' | 'session',小程序时不区分且默认存储
  • rootKey ? string // 可选 缓存的key

getState

获取状态

  • key 状态KEY

dispatch

修改状态

  • key string
  • value any

clean

清除状态

remove

删除某个状态

  • key string

useListen

某个状态发生变化时的HOOK

  • effect: Function // 回调函数,当deps数组中某一项状态发生变化时,会将变化后的值当做参数传入,数据结构像这样{item: 'name', pre: 'jack', next: 'tom'}
    • item 发生变化的状态KEY
    • pre 发生变化之前的数据
    • next 发生变化之后的数据
  • deps: Array // 依赖项,需要监听的状态KEY
  • once: boolean default false // 为true则当依赖项发生变化时只执行一次回调函数就销毁

使用示例:

store.useListen(({item, pre, next}) => {
  console.log(item, pre, next)
}, ['name'], true)

unListen

额外的API

connect

  • 扩展组件
  • 当前组件可以使组件扩展连接其他组件。
  • 扩展的组件可以有一个注入的函数集合,注入的函数在源组件中可以使用this调用
  • 扩展的组件需向外暴露一个对象或函数,对象结构如下,如果是函数,则需返回该对象类型

    注意;扩展组件必须和外界状态无任何关联,即内部状态不依赖props

    注意:源组件必须是函数组件且不能为箭头函数

    interface IInject{
      widget: React.FC<any> // 组件
      injection: { [k: any]: any } // 注入的对象
    }

使用:

connect(injects: Array<IInject | (() => IInject)>)(Component: React.FC<any>)

transmit

  • use为组合API
  • 根组件中将你的store传递下去
  • 同时,可以链式调用globalInjection来注入你的全局组件(可选)

    全局组件:通过useAPI默认注入的组件

在组件中使用 use API来修饰你的组件,你的组件中即可使用this获取到这些东西

import React from 'react'
import MyRouter from '@router'
import store from '@store'
import { transmit } from 'an-store'
...

...
const rootWidget = transmit(store, <MyRouter />)
// 注入全局组件
rootWidget = rootWidget.globalInjection([{ 
  widget: () => <span>测试注入</span>, 
  injection: { 
    add(a, b) {
      console.log('这是注入的函数...')
      return a + b
    }
  }
])
// 你的根组件
React.render(rootWidget, document.getElementById("root"))

注意:此API需放在根组件中执行

use

  • transmit为组合API
  • 使用use来包装你的组件,在组件中则可以使用this来获取到通过transmit传递的store和全局组件

    use(Component)

组件中获取store:

this.store.getState('name')
  • extra 可以使组件再扩展连接其他组件,连接的扩展组件的对象类型也需符合 IInject || () => IInject 类型
use(Component).extra([{ 
  widget: () => <span>注入组件</span> 
  injection: {
    add(a, b) {
      console.log('注入组件函数')
      return a + b
    }
  }
}])
1.0.62

2 years ago

1.0.61

2 years ago

1.0.66

2 years ago

1.0.65

2 years ago

1.0.63

2 years ago

1.0.68

2 years ago

1.0.67

2 years ago

1.0.60

2 years ago

1.0.39

2 years ago

1.0.40

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.0.55

2 years ago

1.0.54

2 years ago

1.0.53

2 years ago

1.0.59

2 years ago

1.0.58

2 years ago

1.0.56

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.38

2 years ago

1.0.16

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.33

2 years ago

1.0.11

2 years ago

1.0.32

2 years ago

1.0.10

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.37

2 years ago

1.0.15

2 years ago

1.0.36

2 years ago

1.0.14

2 years ago

1.0.35

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago