Whats the difference between @observable and @observable.ref modifiers in mobx?
Asked Answered
P

2

6

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 ?

Platinize answered 14/11, 2019 at 6:39 Comment(0)
I
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

Infrequent answered 16/11, 2019 at 11:52 Comment(0)
P
0

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 compare the memory address of the variable which is observed.But when using the @observable, mobx will compare the variable's value to decide to trigger the reactions.

Philibeg answered 5/5, 2022 at 8:4 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewBenzol

© 2022 - 2024 — McMap. All rights reserved.