es2015 arrow functions, fetch and this

1.9k views Asked by At

everyone

I have constructor in class that looks like

 constructor() {
    super();
    const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
    this.state = {
      dataSource: ds.cloneWithRows([])
    };
    let url = Config.baseUrl + "/strains";
    let request = {
      headers: {
        "X-API-Key": Config.apiKey
      }
    };
    fetch(url, request)
      .then((response) => response.json())
      .then((responseData) => {
        if (responseData.data) {
          let strains = responseData.data.map((strain) => {
            return {
              name: strain.name,
              image: strain.image
            };
          });
          this.state = {
            dataSource: ds.cloneWithRows(strains)
          };
          console.dir(this.state);
        }
      });
  }

What I can't get working is this.state assignment inside fetch callback. I'm new to es2015. Read about arrows functions and if I understand it right it should work just fine.

1

There are 1 answers

0
Vladimir Shabunin On

Thanks everybody.

this.setState did the thing.