【问题标题】:How to access DVA Store in UMI application?如何在 UMI 应用程序中访问 DVA Store?
【发布时间】:2020-11-14 06:55:48
【问题描述】:

您好,大多数 React 开发人员都会发现 dvaJS 和 umiJS,它们是状态管理和应用程序开发的天堂。 Dva 是基于elm 的状态管理工具,使用react-redux 进行状态管理。

Q:如何在UMI应用程序中、组件外或没有connect的情况下访问DVA Store?

问:如何dispatchDVA 存储在 UMI 应用程序中,在组件之外或没有connect

【问题讨论】:

    标签: redux umijs dvajs


    【解决方案1】:

    作为和额外的参考以及那些在使用时搜索在react 组件上下文之外访问dva 应用参考的人:

    • Umi.js v3+
    • 连同dva 2.6+(根据框架兼容性)

    可以使用getDvaApp导出的方法:

    import { getDvaApp } from 'umi';
    
    const dispatch = getDvaApp()._store.dispatch;
    
    // use the `dispatch`
    

    【讨论】:

      【解决方案2】:

      要在 UMI 应用中访问或调度 DVA 商店,您可以在功能组件中使用 DVA 挂钩,无需连接。它只能与 DVA v2.6.x 一起使用。

      在功能组件中:

      • 访问商店:
      const state = useSelector(state => state)
      // If state in DVA store changed, rendered values in "state" can be changed in page too.
      

      const store = useStore()
      const state = store.getState()
      // If state in DVA store changed, rendered values in "state" won't be changed in page.
      
      • 调度:
      const dispatch = useDispatch()
      dispatch({type: "namespace/action", payload: 12345})
      

      【讨论】:

        【解决方案3】:

        Q:如何在UMI应用程序中、组件外或没有connect的情况下访问DVA Store?

        答:https://v2.umijs.org/guide/with-dva.html#how-to-access-store-or-dispatch

        上面写着使用:

        window.g_app._store
        

        问:如何dispatchDVA 存储在UMI 应用程序中,在组件之外或没有connect

        答:https://v2.umijs.org/guide/with-dva.html#how-to-access-store-or-dispatch

        上面写着使用:

        window.g_app._store.dispatch('namespace/action')
        

        奖金:

        问:如何get state ofDVA 存储在 UMI 应用程序中,在组件之外或没有connect

        答:https://v2.umijs.org/guide/with-dva.html#how-to-access-store-or-dispatch

        上面写着使用:

        window.g_app._store.getState()
        

        可用功能:

        asyncReducers: {}
        dispatch: ƒ ()
        getState: ƒ f()
        replaceReducer: ƒ (n)
        runSaga: ƒ ()
        subscribe: ƒ subscribe(listener)
        

        推荐:不要直接使用,写一个导出这些函数的Util。

        【讨论】:

          猜你喜欢
          • 2018-10-29
          • 1970-01-01
          • 2012-02-21
          • 1970-01-01
          • 1970-01-01
          • 2022-08-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多