After many attempts I fail to use arrays from https://swapi.co/api/ What I want is to use data from people and films.
I have 2 files : App.js
import React, { Component } from "react";
import List from './List';
const API = 'https://swapi.co/api/';
class App extends Component {
constructor(props) {
super(props);
this.state = {
data: []
};
}
componentWillMount() {
this.fetchData();
}
fetchData = async () => {
const response = await fetch(API);
const json = await response.json();
this.setState({
data: json.data
});
};
render() {
return (
<List data={this.state} />
);
}
}
List.js
import React, { Component } from 'react';
import Person from './Person';
class List extends Component {
render() {
const { data } = this.props;
const { results } = data;
return (
<div className="flex-grow-1">
<div className="row mb-5">{results}</div>
</div>
);
}
}
export default List;
So, how do I go through that array to get what data I want to display ? I'd like to render people -> results and films -> results
<List />component, you pass the data prop asdata={this.state}, so in<List />, this.props.data will be{ data: [{jsondata}] }. You will need to either passdata={this.state.data}to the component, or deconstruct likeconst { data: { data } } = this.propsresultsis undefined when I do<List data={this.state.data} />