tin-dva-hook
v1.0.0
Published
tin-dva-hook 是一个简化dva操作的的hook库。
Downloads
5
Readme
这是简化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 )
}