Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
Mobx observer component doesn't get update after observable change

I have the following MobX class that maintain async operation:

import { makeObservable, observable, action } from "mobx";

class AsyncAction<T, P = void> {
  public isLoading = false;
  public error?: unknown;
  public response?: T;

  constructor(
    private asyncAction: (payload: P) => Promise<T>,
  ) {
    makeObservable(this, {
      error: observable,
      isLoading: observable,
      response: observable,
      setLoading: action,
      setError: action,
      setResponse: action,
    });
  }

setLoading(isLoading: boolean) {
    this.isLoading = isLoading;
  }

  setError(error: unknown) {
    this.error = error;
  }

  setResponse(response: T | undefined) {
    this.response = response;
  }

  async run(payload: P) {
    try {
      this.setLoading(true);
      const response = await this.asyncAction(payload);
      this.setResponse(response);
    } catch (error) {
      this.setError(error);
    } finally {
      this.setLoading(false);
    }
  }
}

export { AsyncAction };

I also have the following store that extends AsyncAction:

import api from '../api';
import { AsyncAction } from "./AsyncAction";

class StatusesStore extends AsyncAction<Record<string, Status>> {
  constructor() {
    super(async () => {
      const { statusesMap } = await api.fetchStatusesMap();
      return statusesMap;
    });

    makeObservable(this, {
      setStatus: action,
    });
  }

  public setStatus(name: string, status: Status) {
    if (this.response) {
      this.response[name] = status;
    }
  }
}

When I trigger from my component rootStore.statusesStore.setStatus('name', 'DONE'), the component doesn't get updated.
When open devools, I see the following: enter image description here Instead of being wrapped with observable, the object keys are plain strings. This might be a reason why changing the status string triggers nothing.
How can I fix that? What am I missing?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Since async processes are resolved in the next tick of the event loop, mobx can't track the changes after the tick. One of the solutions is to use runInAction function after every await keyword.

Like this:

  import {runInAction} from 'mobx'
  async run(payload: P) {
    try {
      this.setLoading(true);// this is okay
      const response = await this.asyncAction(payload);
      // everything after "await" must be wrapped
      runInAction(()=>{
           this.setResponse(response);
      })

    } catch (error) {
      this.setError(error); // wrap in runInAction 
    } finally {
      this.setLoading(false); // wrap in runInAction 
    }
  }

There are also a few other alternatives how you can deal with promises in combination with Mobx. For me, runInAction is the most straightforward way.

For other examples check out official documentation: Asynchronous actions

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!