2

I have a strange problem with a Store I have and I'm trying to fetch data and store that data inside the Store but for some reason no matter how much I try the data isn't persisting. Here is my code.

class FactoryStore extends EventEmitter {

  constructor() {
    super();
    this._factories = [];

    fetch('/factories').then(results => {
      return results.json();
    }).then(data => {
      this._setFactories(data);
      console.log(data); // <------------------- Prints the data as expected
    }).catch(error => console.log(error));
    console.log(this.getFactories()); // <------------ shows nothing, it's empty
  }

  _setFactories(factories) {
    this._factories = factories;
  }

  getFactories() {
    return this._factories;
  }
}

Additionally! I found another answer to go with this. Thanks guys for just marking it as a duplicate!

React Flux Loading Initial Data via API Call

Richard
  • 1,057
  • 1
  • 10
  • 22

0 Answers0