Sep 27, 2020
0
Level 8
React-Native: issue persisting data in mobx store
Hi everybody, I'm working on my first react-native app and I decided to use mobx for state management, I also want to persist some pieces of data so I installed mobx-persit module, I'm trying to get mobx persist to work with the multi store setup I have but I'm getting the following error:
[mobx] Encountered an uncaught exception that was thrown by a reaction or observer component, in: 'Reaction[Reaction@1]', [Error: [serializr] Failed to find default schema for null]
My UserStore is pretty simple so far:
import { observable, action } from "mobx";
import { persist } from 'mobx-persist'
export class UserStore
{
@persist @observable user = {};
@persist @observable users = [];
@action.bound setUser(payload)
{
this.user = payload;
}
@action.bound setUsers(payload)
{
this.users = payload;
}
}
First my store setup uses multiple stores along with react Context API along with a useStores hook, this way I can inject my stores into my components pretty easily:
/contexts/index.js
import React from 'react'
import { configure } from 'mobx';
import { create } from 'mobx-persist'
import { UserStore } from '../stores/UserStore.js'
const hydrate = create({
storage: AsyncStorage,
jsonify: true
})
export const storesContext = React.createContext({
userStore: new UsertStore(),
});
hydrate('userStore', storesContext.userStore).then(() => console.log('UserStore has been hydrated'));
configure({ enforceActions: "observed" });
Than my use-store hook
//hooks/use-stores.js
import React from 'react'
import { storesContext } from '../contexts/index.js'
export const useStores = () => React.useContext(storesContext);
then in my functional components I can do something like this:
import { useStores } from '@hooks/use-stores'
const { userStore } = useStores();
Please or to participate in this conversation.