可以在React Native中坚持MOBX状态树
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论