Mobx计算的吸气剂永远不会被评估

时间:2018-07-10 21:35:34

标签: reactjs typescript mobx

那是我的堆栈闪电/密码笔:https://stackblitz.com/edit/svgtest?file=MountingViewModel.tsx

 import { observable, computed } from 'mobx';

export class MountingViewModel  
{
  constructor(isSelected: boolean, id: string, callback: (mounting: string) => any)
  {
    this.isSelected = isSelected;
    this.id = id;
    this.callback = callback;
  }

  @observable public isSelected: boolean;
  public id: string;
  public callback: Function;

  @computed get opacity() : number
  {
    console.log(this.isSelected);
    return this.isSelected ? 0.2 : 1.0;
  }
}

当我单击红色/蓝色矩形时,永远不会碰到吸气剂opacity()。

那是为什么?

1 个答案:

答案 0 :(得分:1)

  

那是为什么?

因为您正在传播实例,例如<Roof {...this.allMountings.find(x => x.id === "roof") } />

Spread不会调用getter:

class Foo { 
  x = 123;
  get y() {
    return 456
  }
}

console.log({ ...new Foo() }); // {x:123} NO `y`