是否可以直接在我们的 class 或函数中操作和更新存储数据而无需调度操作?

Is it possible to manipulate and update store data directly in our class or functions without dispatching actions?

我在我的 react-native 应用程序中实现了 redux,在单击按钮时我在 mapDispatchToProps 中调度了方法并直接在 action 方法中调用了 API 并更新了商店在具有 API 响应的 reducer 中,并从 mapStateToProps 中的 redux 存储中检索数据。它工作正常。

我们可以从任何 class 中的 redux 存储中检索数据,但我们可以直接从 class 中更新或删除 redux 中的存储值,而无需在 mapDispatchToProps 中调度操作。

是否可以直接在我们的 class 或函数中操作和更新商店数据?

是的,这是可能的。为此,您必须像这样在 class 中导入商店。

import store from ./Filename

//Subscribe your store to get state

store.subscribe(() => console.log(store.getState()))

//dispatch any action from store like following

store.dispatch({ type: 'INCREMENT' })

没有。 The only way you can cause state updates in a Redux app is to dispatch actions to the store.

鉴于模式遵循 reducer 状态保持不可变,dispatch 是旨在保证状态只会在该模式中被替换的层。请记住函数总是 returns 状态,如果使用该状态来呈现更改我不认为绕过它会给出预期的结果。如果不使用调度,也可能存在异步调用,这些调用可能 return 陈旧数据。我认为减速器只是一个函数,所以是的,它可能会以某种方式改变,但可能会发生奇怪的行为。