React Ajax

React + Express(JsonAPI)で嵌りました。

package.jsonにproxyを追加 最後に追加する。

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
   "proxy": "http://localhost:8080"
}

fetch URL は??  同一のサイトになるのでhttp:// は省略する。

componentDidMount() {
    // fetch("https://api.example.com/items")
      fetch("/items")
      .then(res => res.json())
      .then(
        (result) => {
          this.setState({
            isLoaded: true,
            items: result.items
          });
        },
        // Note: it's important to handle errors here
        // instead of a catch() block so that we don't swallow
        // exceptions from actual bugs in components.
        (error) => {
          this.setState({
            isLoaded: true,
            error
          });
        }
      )
  }