简体   繁体   中英

How to do an observable function in Mobx?

I have store with a collection which I want to be read only. I wish to do something like but I don't know if Mobx provides a way to create a reactive function.

class Store{
  private _col:Mobx.Map;
  ...

  @observable public has(id){
    return _col.has(id);
  }       
}

I am on a game architecture with no-trust-the-client in mind. So I don't want my view to get direct access to _col .

@observe
class MyView extends Component {
  ...
  componentWillMount(){
    this.id = this.props.params.id;
    autorun(()=>{
      this.props.store.has(this.id)
      //do something smart
    }
  }

  ...
}

What could be the alternatives?

You can just use

public has(id) { return _col.has(id); }

For observables it doesn't matter whether they are accessed directly or through several layers of indirection, MobX will track it anyways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM