tin-dva-hook
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

这是简化dva操作hook库,方便获取state和loading,dispatch操作

内容

注意: 总共导出两个方法和一个model对象,mergeModel,wrapperModel,model

1. mergeModel(model[])

  • 用于合并两到三个model。
  • 注意model内只能有一个namespace
  • 合并的model里state,reducer,effects,subscriptions等不能有相同的属性key值

2. wrapperModel(model)

  • 这个方法可以导出三个hook,分别是useModelState,useLoading,useCommit
  • 这几个hook分别简化获取state,loading, 和dispatch的操作
  • 对应操作可通过ts提示获取

demo

demo.ts

import { mergeModel,wrapperModel } from 'tin-dva-hook'
import testModel from './testModel

const demoModel = {
  namespace: 'namespace',
  state: {
    list: [],
    data: 1,
  },
  reducers: {
    save(state:any, action:any) {
      return { ...state, ...action.payload };
    }
  },
  effects: {
    *addTodo({ payload }, { call, put, select }) {
      yield put({ type: 'save',payload:{data:payload}});
    },
    *getData({ payload }, { call, put, select }) {
      // 模拟网络请求
      const data = yield call(todoService, payload.id);
      return data; // 返回promise
    },
  },
}
export const {
  model,
  useModelState,
  useLoading,
  useCommit
  } = wrapperModel(mergeModel(demoModel,testModel))
export default model
  调用方式
  import { useModelState, useLoading, useCommit } = from 'demo'

  function Demo(){
    const { list } = useModelState()
    const loading = useLoading('getData') // model异步请求effects的key值
    const commit = useCommit()

    // commit( 'getData',payload:xxx )
  }

Package Sidebar

Install

npm i tin-dva-hook

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

12.6 kB

Total Files

6

Last publish

Collaborators

  • denniszhang