react-native 与 mobx 抛出错误
react-native with mobx throw error
我用 mobx 实现了本机反应,但我得到了这个错误
[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
根组件
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;
应用商店
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;
用户商店
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;
我已经尝试做的是删除应用程序并再次 运行,但仍然没有任何改变,它抛出同样的错误。
问题是
@persist @observable storeHydrated = false;
UserStore 内部不应保留,因为它是每次加载应用程序时都会重新启动的值。
因为它是持久化的,所以 true 的值在第一个 运行 之后保存在存储中。
所以使用:
@observable storeHydrated = false;
然后 StackNavigator Main 在您定义它之前呈现。
额外提示:
当使用 @persist @observable varName
时,
你应该总是初始化一个值,甚至是 null,因为 'undefined' 的序列化不被正确支持所以它应该看起来像这样:
@persist @observable varName = null
(或任何适合空字符串、0、真、假等的东西)
对于特殊方案,它应该是:
@persist('list') @observable varName = []
@persist('object') @observable varName = null
@persist('map') @observable varName = new Map()
我用 mobx 实现了本机反应,但我得到了这个错误
[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
根组件
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;
应用商店
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;
用户商店
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;
我已经尝试做的是删除应用程序并再次 运行,但仍然没有任何改变,它抛出同样的错误。
问题是
@persist @observable storeHydrated = false;
UserStore 内部不应保留,因为它是每次加载应用程序时都会重新启动的值。
因为它是持久化的,所以 true 的值在第一个 运行 之后保存在存储中。
所以使用:
@observable storeHydrated = false;
然后 StackNavigator Main 在您定义它之前呈现。
额外提示:
当使用 @persist @observable varName
时,
你应该总是初始化一个值,甚至是 null,因为 'undefined' 的序列化不被正确支持所以它应该看起来像这样:
@persist @observable varName = null
(或任何适合空字符串、0、真、假等的东西)
对于特殊方案,它应该是:
@persist('list') @observable varName = []
@persist('object') @observable varName = null
@persist('map') @observable varName = new Map()