Hi, I'm David

Welcome to my tech blog

Async Calls with React

2019-10-19 david shiblog

Almost a year later! unsplash.com

Development, Android, IOS, and getting into stores.

Thank you REACT! Compiling into code into web, native ios, native android is definately a plus. I can’t make everything with React, but 99% of the projects is doable with it, saving a tonne of time with Web + App bundles.

One of favorite little snippets to handle any sort of async calls:

  async getResturantsFromApiAsync(city) {
    try {
      const response = await fetch(`https://opentable.herokuapp.com/api/restaurants?city=${city}`);
      if (!response.ok) {
        this.setState({ error: response.statusText });
      }
      const json = await response.json();
      this.setState({ results: json });
    } catch (error) {
      this.setState({ error: error });
    }
  }

Grabs the data, if it’s okay, post it into your state. Voila, very simple. Works well.

A working example can be seen here. Instructions to compile, run, and build it are also on that page. Simple Async Search by David Shi