可以在React Native中坚持MOBX状态树

发布于 2025-02-10 08:10:10 字数 2521 浏览 1 评论 0原文

我正在使用 mobx-state-tree 用于状态管理和 mst-persist 坚持我的数据。问题是,当我重新加载应用程序时,商店的初始数据首先渲染,然后加载持久数据。 因此,每当我想在商店中检查持久数据时,初始数据渲染器首先,我的功能就会基于此运行,当一切都停止渲染我的持久数据渲染器时。 此代码是我问题的简化版本。当应用首先渲染时,我会在控制台中获得“ false”,然后我得到“ true”。即使我评论 settemp()。 有什么方法可以解决此问题,还是可以使用其他软件包来持续使用MST?

rootstore.ts

import {
  types,
  Instance,
  applySnapshot,
  getSnapshot,
} from 'mobx-state-tree';
import {createContext, useContext} from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {persist} from 'mst-persist';


const RootStore = types
  .model('RootStore', {
    temp: false,
  })
  .actions(store => ({
    setTemp() {
      applySnapshot(store, {...getSnapshot(store), temp: true});
    },
  }));
  
  let _store: any = null;

export function initializeStore() {
  _store = RootStore.create({});
  persist('@initstore', _store, {
    storage: AsyncStorage,
    jsonify: true,
    whitelist: ['temp'], 
  });

  return _store;
}
export type RootInstance = Instance<typeof RootStore>;
const RootStoreContext = createContext<null | RootInstance>(null);
export const Provider = RootStoreContext.Provider;

export function useStore(): Instance<typeof RootStore> {
  const store = useContext(RootStoreContext);
  if (store === null) {
    throw new Error('Store cannot be null, please add a context provider');
  }
  return store;
}

app.ts

import {initializeStore,Provider} from './src/store/RootStore';

const store = initializeStore();

<Provider value={store}>
  <RootStack /> //the App
</Provider>

初始化屏幕

import {observer} from 'mobx-react-lite';
import {useStore} from '../../store/RootStore';

const InitializeScreen = observer((): JSX.Element => {
  const {setTemp,temp} = useStore();
  useEffect(() => {
    setTemp()
  }, []);

  console.log('init page',temp); // <-- Every time app reloads reneders false then true
  
  
  return (
    <Text>InitializeScreen</Text>
  );
});

export default InitializeScreen;

I am using MobX-state-tree for state management and mst-persist to persist my data. The problem is when I reload the app the initial data of the store renders first and then the persist data gets loaded.
So whenever I want to check a persisted data in my store the initial data renders first, My function runs based on that and when everything stopped rendering my persisted data renders.
This code is a simplified version of my problem. When app renders first I get "false" in my console then I get "True". Even after I comment out the setTemp().
Is there any way to fix this or is there another package that I can use for persisting MST?

Rootstore.ts

import {
  types,
  Instance,
  applySnapshot,
  getSnapshot,
} from 'mobx-state-tree';
import {createContext, useContext} from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {persist} from 'mst-persist';


const RootStore = types
  .model('RootStore', {
    temp: false,
  })
  .actions(store => ({
    setTemp() {
      applySnapshot(store, {...getSnapshot(store), temp: true});
    },
  }));
  
  let _store: any = null;

export function initializeStore() {
  _store = RootStore.create({});
  persist('@initstore', _store, {
    storage: AsyncStorage,
    jsonify: true,
    whitelist: ['temp'], 
  });

  return _store;
}
export type RootInstance = Instance<typeof RootStore>;
const RootStoreContext = createContext<null | RootInstance>(null);
export const Provider = RootStoreContext.Provider;

export function useStore(): Instance<typeof RootStore> {
  const store = useContext(RootStoreContext);
  if (store === null) {
    throw new Error('Store cannot be null, please add a context provider');
  }
  return store;
}

App.ts

import {initializeStore,Provider} from './src/store/RootStore';

const store = initializeStore();

<Provider value={store}>
  <RootStack /> //the App
</Provider>

InitializeScreen.ts

import {observer} from 'mobx-react-lite';
import {useStore} from '../../store/RootStore';

const InitializeScreen = observer((): JSX.Element => {
  const {setTemp,temp} = useStore();
  useEffect(() => {
    setTemp()
  }, []);

  console.log('init page',temp); // <-- Every time app reloads reneders false then true
  
  
  return (
    <Text>InitializeScreen</Text>
  );
});

export default InitializeScreen;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文