React-Native:什么时候在应用程序中使用 redux state 和 react state? [英] React-Native: when to go for redux state and react state in app?
问题描述
我已经在我的应用程序中实现了 redux
.我的场景是从一个组件导航到另一个组件时,我正在更新 redux
状态.所以当我回到上一个组件时,获取最后一个 redux
状态数据.
I have implemented redux
in my application. My scenario is when navigating from one component to another component, I'm updating the redux
state. So when I come back to the previous component, getting the last redux
state data.
预期结果:
当我回到上一个组件时,需要处于相同的状态.我不想要最近的更改数据.
When I come back to previous component, need to be in same state. I don't want the recent changes data.
Ex: Marie ->April, when I go back, Marie <- April
实际结果:
它正在更新最近的更改数据.
Its updating the recent changes data.
Ex: Marie ->April, when I go back, April <- April
我该如何为此编写应用程序结构?这里需要 redux
吗?还是我应该在导航时通过作为 params
传递来管理组件状态?
How could I write a app structure for this? Is it redux
needed here? or Should I manage along with component state by passing as a params
when navigating?
推荐答案
我不确定我的答案是否是正确的实施方式.但目前,我是这样做的:
State
可以分散在多个位置(例如:this.state
、类的字段、全局对象或 Redux).
State
of a component can be scattered through multiple locations (ex: this.state
, class's fields, a global object or Redux).
我从您的问题中了解到您同时处理 2 个要求:
What I understand from your question is that you are dealing with 2 requirements simultaneously:
- 组件的部分状态需要保留的时候导航回来和福特.
- 组件状态的另一部分需要与导航返回和福特时的其他组件(保持不变).
我不知道针对这两个要求的单一解决方案.如果我必须这样做,我将设计组件的状态,使其分布或分散到不同的位置,每个位置都有自己的目的.基本上,我使用两种不同的解决方案:
I am not aware of a single solution for both requirements. If I have to do it I will design the state of the component in such a way that it is distributed or scattered through different locations each serves its own purposes. Basically, I use 2 different solutions:
- 需要保留的部分状态将被放入
this.state
. - 需要共享的部分状态会放在
Redux
中.
为了让这个解决方案起作用,我不使用 react-redux,只使用 redux
.在 redux
中有一个方法叫做 store.getState() 将返回单个 Redux 状态对象.我将 store
设为全局只读,并使用它来呈现需要共享数据的组件部分.
For this solution to work, I do not use react-redux, only redux
. In redux
there is a method called store.getState() which will return the single Redux state object. I make the store
read-only-accessible globally and use it to render parts of the component that need the shared data.
这篇关于React-Native:什么时候在应用程序中使用 redux state 和 react state?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!