1.js
export default class Service {
_apiBase = 'https://swapi.co/api';
async getResource(url) {
const res = await fetch(`${this._apiBase}${url}`);
return await res.json();
}
async getAllPeople() {
const res = await this.getResource(`/people/`);
return res.results
// .map(results => results.name);
}
}
2.js
import React, { Component } from 'react';
import Service from './Services';
export default class App extends Component {
service = new Service();
state = {
person: null
}
componentDidMount() {
this.service.getAllPeople()
.then((res) => {
this.setState({
person: res,
})
console.log(res.map(results => (
results.name
)));
})
}
render() {
return (
);
}
}