可观察子类化的扩展:[MobX] 不能为已经可观察的对象提供选项

发布于 2025-01-12 01:54:04 字数 1433 浏览 3 评论 0原文

当我创建一个具有很少扩展的深层结构时,我收到了这种错误:

未捕获错误:[MobX]无法为已经可观察的对象提供选项。

"mobx": "^6.4.2" , "mobx-react-lite": "^3.3.0",

所有代码只是肮脏的例子。真实的结构更加复杂。

代码示例:





import { makeObservable, observable, action } from 'mobx';

class DateMobx {
    date ;

    constructor(data) {
        this.date = new Date(data.date);
        makeObservable(this, { date: observable }, { autoBind: true });
    }
}

class Todo extends DateMobx {
    id = 0;
    title = 'title';
    text = 'text';

    constructor(todo) {
        super(todo);
        this.id  = todo.id;
        this.title  = todo.title;
        makeObservable(this, { id: observable,  title: observable, changeTitle: action }, { autoBind: true });
    }

    changeTitle= (e) => {
        const { value } = e.target;
        this.title = value;
    }
}
class TodoList {
    todo = [];

    constructor() {
        const todoData = [{ id: 1, title: 'title', date: '123' }];
        this.todo = todoData.map(item => new Todo(item)); // ERROR happen here

        makeObservable(this, { todo: observable }, { autoBind: true });
    }
}


 TodoList 类的构造函数中发生错误。

如果从 Todo 类中删除 makeObservable ,则不会重现错误,但我需要该类中的反应性。

如果从 Todo 类中删除 extends DateMobx ,错误也不会重现(但我有很多具有基本逻辑的通用类,我也需要反应性)。


 为什么会发生这种情况?如果我真的需要这种深层结构该怎么办?

When I create a deep structure with few extends I got this kind of error:

Uncaught Error: [MobX] Options can't be provided for already observable objects.

"mobx": "^6.4.2",
"mobx-react-lite": "^3.3.0",

All of code is just dirty example. real structure more complex.

Code example:





import { makeObservable, observable, action } from 'mobx';

class DateMobx {
    date ;

    constructor(data) {
        this.date = new Date(data.date);
        makeObservable(this, { date: observable }, { autoBind: true });
    }
}

class Todo extends DateMobx {
    id = 0;
    title = 'title';
    text = 'text';

    constructor(todo) {
        super(todo);
        this.id  = todo.id;
        this.title  = todo.title;
        makeObservable(this, { id: observable,  title: observable, changeTitle: action }, { autoBind: true });
    }

    changeTitle= (e) => {
        const { value } = e.target;
        this.title = value;
    }
}
class TodoList {
    todo = [];

    constructor() {
        const todoData = [{ id: 1, title: 'title', date: '123' }];
        this.todo = todoData.map(item => new Todo(item)); // ERROR happen here

        makeObservable(this, { todo: observable }, { autoBind: true });
    }
}


Error happen in constructor of TodoList class.


if remove makeObservable from Todo class, error is not reproduced but I need reactivity in that class.


If remove extends DateMobx from Todo class error also is not reproduces (but I have a lot of general classes with basic logic where I need reactivity too).


Why its happen and what should I do if I really need such kind of deep structure ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妞丶爷亲个 2025-01-19 01:54:04

github 上的人给我带来了正确的解决方案

不要传递第三个参数 ({ autoBind: true }) 到子类中的 makeObservable。所有选项都是“继承的”,并且不能通过对同一对象 (this) 的后续 makeObservable 调用来更改。

选项参数只能提供一次。传递的选项有
“粘性”并且以后不能更改(例如在子类中)。

https://mobx.js.org/observable-state.html#limitations

Guys on github bring me the right solution

Don't pass the third param ({ autoBind: true }) to makeObservable in subclasses. All options are "inherited" and can't be changed by subsequent makeObservable calls on the same object (this).

options argument can be provided only once. Passed options are
"sticky" and can NOT be changed later (eg. in subclass).

https://mobx.js.org/observable-state.html#limitations

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文