反应原生mobx值不是原始的

时间:2018-04-07 19:58:00

标签: react-native mobx mobx-react

我构建反应本机应用程序,我使用mobx。 进入商店我有数组称为工具,当我尝试将值分配到数组我得到数组

  

遇到由反应或观察者组件抛出的未捕获异常,在:'反应[反应@ 1]错误:[serializr]此值不是原始的:14

 class Task {
@persist @observable id = ''
@persist @observable title = ''
@persist @observable description = ''
@persist @observable tools = []

constructor(id,title,description,tools){
    console.log('new task')
    console.log(id,title,description,tools)
    this.id = id
    // this.title = title
    this.description = description
    this.tools = tools;

}

TasksStore - 这里我创建了Task的新对象。

    import { observable, action, computed } from 'mobx'
import { persist } from 'mobx-persist'
import Task from '../Task/Task'
class TasksStore {

    @persist('list',Task) @observable tasks = []
    @observable storeHydrated = false;


    @action done(){
        this.storeHydrated = true

    }

    @action addNewTask(task){
        this.tasks.push(new Task(0,task.title,task.description,task.tools))
        console.log('tasks is',this.tasks)
    }






}


const taskStore = new TasksStore();
export default taskStore;

当我删除

    @persist @observable tools = []

该应用程序运行良好。

2 个答案:

答案 0 :(得分:2)

@persist('list') @observable tools = [] 

尝试设置工具类型。 会起作用

答案 1 :(得分:0)

我遇到了同样的问题。解决方法是使Task类像这样可序列化:

class Task {
 @serializable @observable id = ''
 @serializable @observable title = ''
 @serializable @observable description = ''
 @serializable(list(primitive()))   @observable tools = []

constructor(id,title,description,tools){
  console.log('new task')
  console.log(id,title,description,tools)
  this.id = id
  // this.title = title
  this.description = description
  this.tools = tools;  
}

并将您现在可序列化的任务保留在您的商店中:

class Store{
  @persist('object', Task)
  @observable
  public Task= new Task()
}