简体   繁体   English

状态如何在React Native中将数据存储在Redux中?

[英]How the state will store the data in redux in react native?

我想知道,如果我们将数据以某种状态存储在redux中,并且突然将数据更新为redux,则先前的状态将被覆盖,或者将其维护为副本或类似版本(v1,v2),有人可以吗指导我吗?

The previous states will not be stored, once you close the app the "session" is cleared, minimizing the app will see the state persists, there are a number of options for permanent data storage, redux-persist is one of them, and probably the easiest to implement, there is also a built in option with react-native (this is however the worst option as i find it often causes my apps to hang if a process takes too long, i suspect it's blocking the js thread). 以前的状态将不会存储,一旦关闭应用程序,“会话”将被清除,将应用程序最小化将看到状态持续存在,永久性数据存储有很多选项,redux-persist是其中之一,并且可能最容易实现的是,还有一个带有react-native的内置选项(但是这是最糟糕的选项,因为我发现如果一个进程花费太长时间,它通常会导致我的应用程序挂起,我怀疑它阻塞了js线程)。 The best option, but slightly more difficult to setup is https://realm.io/ which i use in conjunction with redux and saga, this is great for an offline first approach to building your app, as you can check users connectivity and either make calls to your api, or to your realm storage. 最佳选择是https://realm.io/ ,它是我与redux和saga结合使用的最佳选择,但设置起来有点困难,这对于离线构建应用程序的第一种方法非常有用,因为您可以检查用户的连接性,或者调用您的api或您的领域存储。

See code example below for appending data in reducer and replacing data: 请参见下面的代码示例,以在减速器中追加数据并替换数据:

  case NEXT_PAGE_SUCCESS:
      return {
        ...state,
        busy: false,
        msg: action.payload.statusText,
        status: true,
        data: [...state.data, ...action.payload.data]
      }

In the above reducer case, ...state will drop in the previous state, now afterwards we also assign busy: false, this will overwrite the old busy state. 在上面的reducer情况下,... state将下降到先前的状态,现在我们还要分配busy:false,这将覆盖旧的busy状态。

Now if you look at the data field, we have: 现在,如果您查看数据字段,我们将:

[...state.data,...action.payload.data]

This will combine the previous state.data, with the new state.data appending the list instead of overwriting. 这将合并先前的state.data和新的state.data而不是覆盖列表。

I hope this answers your question? 我希望这回答了你的问题?

Lloyd 劳埃德

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM