I implement react native with mobx and i get this errorr
[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 undefined
root component
import React, { Component } from 'react';
import {
View,
AsyncStorage,
Text,
} from 'react-native';
import { StackNavigator } from 'react-navigation';
import { observer, Provider } from 'mobx-react/native'
import { create } from 'mobx-persist'
import { AppStore,UserStore } from '../stores'
const stores = { UserStore, AppStore };
import {
Walkthrough
} from '../screens'
const hydrate = create({
storage: AsyncStorage
})
let Main = null;
hydrate('appStore', AppStore).then((a) => {
console.log(a);
AppStore.done();
}
)
hydrate('userStore',UserStore).then((a)=>{
console.log(a)
Main = StackNavigator(
{
Walkthrough: { screen: Walkthrough },
}, {
headerMode: 'none',
lazy: true,
initialRouteName: 'Walkthrough',
}
)
})
@observer
class Root extends Component {
constructor(props){
super(props);
this.state = {
}
}
render(){
// const ready = UserStore.storeHydrated && AppStore.storeHydrated;
const ready = false;
return (
<View>
{!ready &&
<View><Text>Splash screen component</Text></View>}
</View>
)
}
}
export default Root;
AppStore
import { observable, action, computed } from 'mobx'
import { persist } from 'mobx-persist'
class AppStore {
@observable storeHydrated = false
@observable requesting = false
constructor(){
console.log("AppStore")
}
@action setRequesting(){
this.requesting = !this.requesting;
}
@action done(){
//this.storeHydrated = true
}
}
const appStore = new AppStore();
export default appStore;
UserStore
import { observable, action, computed } from 'mobx'
import { persist } from 'mobx-persist'
class UserStore {
@persist @observable first_name;
@persist @observable last_name;
@persist @observable email;
@persist @observable tasks;
@persist @observable storeHydrated = false;
@persist @observable alreadyRegister = false;
constructor(){
this.setUser = this.setUser.bind(this);
console.log("userStore")
}
@action setUser(data){
this.first_name = data.first_name
this.last_name = data.last_name;
this.email = data.email;
this.tasks = data.tasks;
}
@action done(){
//this.storeHydrated = true;
// if(this.alreadyRegister){
// this.getUserDetails();
// }
}
@action setIsFirstTimeInApp(firstTIme){
this.firstTimeInApp = firstTIme;
}
}
const user = new UserStore();
export default user;
what I already tried to do is to delete the app and run again and still nothing changed and it throw me the same error.
the problem is
@persist @observable storeHydrated = false;
inside the UserStore should not be persist since it's a value the restarts on every load of the app.
since it's in persist the value of true is saved in storage after the first run.
so use:
@observable storeHydrated = false;
then the StackNavigator, Main, is rendered before you have defined it.
extra tip:
when using @persist @observable varName
,
you should always initialize a value, even null, since the serialization of 'undefined' is not supported properly so it should look like this:
@persist @observable varName = null
(or any thing that fits like empty string , 0 , true , false etc.)
and for special schemes it should be:
@persist('list') @observable varName = []
@persist('object') @observable varName = null
@persist('map') @observable varName = new Map()