Skip to content

How to call disposers on result of computed property? #2560

Answered by urugator
brabeji asked this question in Q&A
Discussion options

You must be logged in to vote

Possibility to consider

class ElementSize {
  resizeObserver: ResizeObserver;
  atom: Atom;
  @observable observed: boolean = false,
  @observable ref: ObservableRefObjec<HTMLElement>

  constructor(ref: ObservableRefObject<HTMLElement>) {
    this.ref = ref;
    this.resizeObserver = new ResizeObserver(() => this.atom.reportChanged());  

    this.atom = mobx.createAtom(
       'ElementSize',
       () => this.observed = true
       () => this.observed = false
    )   
    
    autorun(() => {
      if (this.ref.current && this.observed) {
        this.resizeObserver.observe(this.ref.current);
      } else {
        this.resizeObserver.disconnect()
      }
    })
  }

  @computed get width(

Replies: 3 comments 10 replies

Comment options

You must be logged in to vote
3 replies
@brabeji
Comment options

@urugator
Comment options

@brabeji
Comment options

Comment options

You must be logged in to vote
2 replies
@brabeji
Comment options

@omerman
Comment options

Comment options

You must be logged in to vote
5 replies
@brabeji
Comment options

@omerman
Comment options

@brabeji
Comment options

@urugator
Comment options

@omerman
Comment options

Answer selected by brabeji
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants