反复原生与mobx throw错误

时间:2018-02-17 21:22:11

标签: react-native mobx mobx-react mobx-persist

我实现了与mobx的本机反应,我得到了这个错误

  

[mobx]遇到一个由反应或观察者组件抛出的未捕获异常,在:'反应[Reaction @ 1]错误:[serializr]无法找到未定义的默认架构

根组件

    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;

我已经尝试做的是删除应用程序并再次运行,但仍然没有任何改变,它给我带来了同样的错误。

1 个答案:

答案 0 :(得分:2)

问题是

@persist @observable storeHydrated = false;
UserStore中的

不应该是持久的,因为它是应用程序每次加载时重新启动的值。

因为它持续存在,所以在第一次运行后,true的值会保存在存储中。

所以使用:

@observable storeHydrated = false;

然后在定义之前渲染StackNavigator,Main。

额外提示:

使用@persist @observable varName时, 你应该总是初始化一个值,甚至是null,因为未定义的序列化&#39;不正确支持所以它应该是这样的:  @persist @observable varName = null(或任何适合空字符串,0,true,false等的东西) 对于特殊计划,它应该是:

@persist('list') @observable varName = [] 
@persist('object') @observable varName = null
@persist('map') @observable varName = new Map()