简体   繁体   中英

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). 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.

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.

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.

I hope this answers your question?

Lloyd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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