【发布时间】:2020-11-14 06:55:48
【问题描述】:
您好,大多数 React 开发人员都会发现 dvaJS 和 umiJS,它们是状态管理和应用程序开发的天堂。 Dva 是基于elm 的状态管理工具,使用react-redux 进行状态管理。
Q:如何在UMI应用程序中、组件外或没有connect的情况下访问DVA Store?
问:如何dispatchDVA 存储在 UMI 应用程序中,在组件之外或没有connect?
【问题讨论】:
您好,大多数 React 开发人员都会发现 dvaJS 和 umiJS,它们是状态管理和应用程序开发的天堂。 Dva 是基于elm 的状态管理工具,使用react-redux 进行状态管理。
Q:如何在UMI应用程序中、组件外或没有connect的情况下访问DVA Store?
问:如何dispatchDVA 存储在 UMI 应用程序中,在组件之外或没有connect?
【问题讨论】:
作为和额外的参考以及那些在使用时搜索在react 组件上下文之外访问dva 应用参考的人:
可以使用getDvaApp导出的方法:
import { getDvaApp } from 'umi';
const dispatch = getDvaApp()._store.dispatch;
// use the `dispatch`
【讨论】:
要在 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})
【讨论】:
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。
【讨论】: