store.ts
import { createStore } from 'ice'
import user from './models/user'
const store = createStore(
{
user,
},
{}
)
export default store
models/user.ts
export default {
// 定义 model 的初始 state
state: {
msg:'工商查询 印章管理 预警通知'
},
// 定义改变该模型状态的纯函数
reducers: {
update(prevState, payload) {
return {
...prevState,
...payload,
};
},
},
// 定义处理该模型副作用的函数
effects: (dispatch) => ({
async getMsg(data) {
dispatch.user.update({
msg: data
});
},
}),
};
index.tsx
import store from '@/store'
const adminForm = () => {
const [userState, userDispatchers] = store.useModel('user')
console.log(userState) //{msg: '工商查询 印章管理 预警通知'}
// 初始化时清除定时器
useEffect(() => {
console.log(userDispatchers) // {update: ƒ, setState: ƒ, getMsg: ƒ}
userDispatchers.update({msg: '11111'} )
console.log(userState) //{msg: '11111'}
}, [])
}