本文介绍了Redux 状态在 React Native 中在内存中持续多长时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何在我的 react-native 应用程序中管理 redux 状态.特别是,我试图弄清楚当我的应用最小化或设备关闭时会发生什么.

I am trying to understand how to manage redux state in my react-native app. In particular, I am trying to work out what happens when my app is minimized or the device is turned off.

有了这些知识,我就可以找出最好的方法来持久化状态,并为重置 redux 状态可能产生的任何非法状态做好准备.

With this knowledge I can then work out best how to persist state, and prepare for any illegal states that may arise from the redux state being reset.

如果 redux 状态被重置,整个应用程序本身将重新启动(因此任何 redux 存储的初始化都将被再次调用?)是否公平假设.

Is it a fair assumption to make that if the redux state is reset, the entire app itself will restart (and therefore any initialization of the redux store will be called again?).

推荐答案

这个假设是不正确的,但反过来却是.如果有代码可以通过重置操作或其他方式来重置 redux 状态,则可以在运行时重置它.

This assumption is not true, however the inverse is. The redux state can be reset during runtime if there is code to do it via a reset action or otherwise.

但是,如果应用程序关闭,并且代码重新运行,它将以您的默认 redux 状态加载.

However if the app is shut down, and the code re-runs it will be loaded with your default redux state.

AsyncStorage(原生)/localstorage for web,是通常与 redux 结合使用,以便在应用崩溃/浏览器会话中保持状态.

AsyncStorage (in native) / localstorage for web, is often used in conjunction with redux in order to persist state across app crashes / browser sessions.

您可以将 AsyncStorage 设置为 redux-persist 中间件的策略(或编写您的自己的)

You can set AsyncStorage as a strategy for the redux-persist middleware (or write your own)

这篇关于Redux 状态在 React Native 中在内存中持续多长时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 21:28