You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
1.2 KiB

2 years ago
  1. // 组合子模块(根组件)
  2. // 封装统一导出的供业务使用的方法
  3. import React from 'react'
  4. import { ListStore } from "./list.Store";
  5. import { CounterStore } from "./counter.Store"
  6. // 1.声明一个 RootStore
  7. class RootStore {
  8. constructor() {
  9. // 对子模块进行实例化操作
  10. // 将来实例化根 RootStore 的时候,根 RootStore 有两个属性,分别是 CounterStore 和 ListStore,各自对应的值就是导入的子模块实例对象
  11. this.counterStore = new CounterStore()
  12. this.listStore = new ListStore()
  13. }
  14. }
  15. // 实例化操作
  16. RootStore = new RootStore()
  17. // 使用 react context 机制,完成统一的方法封装
  18. // 通过 Provider value={传递的数据} 的查找机制:当调用 useContext,优先从 Provider value 找,如果找不到,就会 createContext 方法传递过来的默认参数
  19. const context = React.createContext(RootStore)
  20. // 这个方法作用:通过 useContext 拿到 RootStore 实例对象,然后返回,所以只要在业务组件中,调用 useStore(),将获得一个跟实例对象 RootStore
  21. const useStore = () => React.useContext(context)
  22. export { useStore }