Использование fetch для получения данных из API в React - PullRequest
0 голосов
/ 01 июня 2018

В Panel.js как я могу получить данные из API, который я написал в index.js?

Panel.js enter image description here

index.jsenter image description here

ProfilePanel.js enter image description here

Ответы [ 2 ]

0 голосов
/ 01 июня 2018

вы уже получили ответ и сопоставили ответ с состоянием 'results'. Вы можете отобразить данные, просто написав "this.state.results"

0 голосов
/ 01 июня 2018

Вы можете использовать API выборки JavaScript

Пример GET

fetch(`https://jsonplaceholder.typicode.com/posts?key=${key}&results=${results}`)
            .then((res) => { return res.json() })
            .then((data) => { 
                console.log(data)
                });
           })
}

Пример POST

  let key = 'key'
  let results = 12;

  fetch('https://jsonplaceholder.typicode.com/posts', {
      method: 'POST',
      headers : new Headers(),
      body:JSON.stringify({key: key, results: results})//use property short hand here
  }).then((res) => res.json())
  .then((data) =>  console.log(data))
  .catch((err)=>console.log(err))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...