为什么我的 React Native 应用程序的状态在 iOS 上切换应用程序时被清除,但当应用程序完全关闭并重新打开时仍然存在?

问题描述 投票:0回答:1

此问题是在升级到最新架构后出现的。 此外,只有某些部分被清除,例如loggedInUser和主页列表。

Environment and Setup:

Device:
iPhone 16 Pro, iOS 18.1 Simulator and in Actual Testflight

React Native Version:
0.76.3 with New Architecture Enabled

Other Relevant Libraries:
mobx: "^6.13.5",
mobx-react: "^9.1.1",
react-navigation: 7.x.x

ios react-native react-navigation mobx
1个回答
0
投票

修复了 MobX 存储初始化逻辑

[旧代码]

let store: RootStore;

// create the context
const StoreContext = createContext<RootStore | undefined>(undefined);

// create the provider component
export function RootStoreProvider({ children }: { children: ReactNode }) {
  //only create the store once ( store is a singleton)
  const root = store ?? new RootStore();

  return <StoreContext.Provider value={root}>{children}</StoreContext.Provider>;
}

[新代码]

// Singleton store variable
let store: RootStore | undefined;

// Create the context
const StoreContext = createContext<RootStore | undefined>(undefined);

// RootStoreProvider Component
export function RootStoreProvider({ children }: { children: ReactNode }) {
  // Initialize the store only once
  if (!store) {
    store = new RootStore();
  }

  return <StoreContext.Provider value={store}>{children}</StoreContext.Provider>;
}

© www.soinside.com 2019 - 2024. All rights reserved.