0

How can I set the value of mobx store from server side as initial value (from fetch/http)?

import { computed, observable } from 'mobx';
import { Instrument } from '../models/core/instrument';

class ObservableInstrumentStore {
  //should set init value from server instead of setting to empty array
  @observable instruments: Instrument[] = [];

  @computed get getInstruments(): Instrument[] {
    return this.instruments;
  }

  addInstruments(instruments: Instrument[]): void {
    this.instruments = instruments;
  }
}

export const observableInstrumentStore = new ObservableInstrumentStore();
Arun Gopalpuri
  • 2,340
  • 26
  • 27

1 Answers1

0

How would you do it you weren't using mobx and observables?

There are some options.

  1. Wait for the array to have length bigger than zero in order to do something with the data.

  2. Have a flag that signals that loading of data is finished and then try to do something with the data.

Ivan V.
  • 7,593
  • 2
  • 36
  • 53