最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Whats the difference between @observable and @observable.ref modifiers in mobx? - Stack Overflow

programmeradmin4浏览0评论

Mobx supports both @observable and @observable.ref modifiers and their official doc says

observable: This is the default modifier, used by any observable. It clones and converts any (not yet observable) array, Map or plain object into it's observable counterpart upon assignment to the given property

observable.ref: Disables automatic observable conversion, just creates an observable reference instead.

I do not understand why would we need to create a observable reference for an already existing observable. How are these two different and what are their use cases ?

Mobx supports both @observable and @observable.ref modifiers and their official doc says

observable: This is the default modifier, used by any observable. It clones and converts any (not yet observable) array, Map or plain object into it's observable counterpart upon assignment to the given property

observable.ref: Disables automatic observable conversion, just creates an observable reference instead.

I do not understand why would we need to create a observable reference for an already existing observable. How are these two different and what are their use cases ?

Share Improve this question asked Nov 14, 2019 at 6:39 freakomonkfreakomonk 6141 gold badge7 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 16

observable.ref will only track reference changes to the object, which means that you will need to change the whole object in order to trigger the reaction. So if you, for example, have an observable array that is tracked by reference, the contents of the array will not be tracked. Also if you add items to the array that change will also not be tracked.

import { observable, reaction } from "mobx";

const test = observable(
  {
    reference: [{ id: 1 }]
  },
  {
    reference: observable.ref
  }
);

reaction(
  () => {
    return test.reference.length;
  },
  () => {
    console.log("Reaction 1: reference length changed");
  }
);
reaction(
  () => {
    return test.reference[0];
  },
  () => {
    console.log("Reaction 2: reference item changed");
  }
);

test.reference[0] = { id: 2 }; // this will not trigger the reactions
test.reference.push({ id: 3 }); // this will not trigger the reactions
test.reference.pop(); // this will not trigger the reactions

test.reference = []; // this will trigger both reactions

code sandbox example

I'd like to explain the Ivan V.'s answer. In his code, test.reference is a reference which will not change the address of the variable in the memory until reassign. operations like push/pop or edit the value of test.reference[0] just change the value instead of the variable's reference. So when using the @observable.ref, mobx will pare the memory address of the variable which is observed.But when using the @observable, mobx will pare the variable's value to decide to trigger the reactions.

发布评论

评论列表(0)

  1. 暂无评论