dagatsoin
dagatsoin

Reputation: 2656

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?

Upvotes: 1

Views: 2462

Answers (1)

mweststrate
mweststrate

Reputation: 4978

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.

Upvotes: 3

Related Questions