Reputation: 912
I have only been working with React for two days and I am stuck on displaying the results of a fetch for a JSON file. When I do a console.log, the data appears in the console, yet I cannot figure out for the life of me, how to display it back through the render function.
Also, I can not figure out how to pass or set the value of a variable. In this case, I am receiving warning - "data is assigned a value but never used"
I have tried { JSON.stringify(this.Songs, null, 2) }. I have tried to call this.data[0].title to display just the first title in the JSON, but I receive an error that 0 is undefined.
It is like the value of this.data or this.Songs is lost outside of it's original function.
I have tried to use let data, const data, this.data = this.data.bind, etc...
How can I make the values available for render()? And how do I render them to page?
Here's the code and thanks in advance:
import React from 'react';
class Songs extends React.Component {
constructor(){
super();
const data = [];
}
componentDidMount() {
fetch('http://localhost:3000/data.json')
.then((response) => response.json())
.then((responseJson) => {
this.Songs = responseJson;
this.data = responseJson;
this.getData(this.Songs);
this.createColumns();
})
.catch((error) => {
console.error(error);
});
}
createColumns() {
this._columns = ['id', 'title', 'original_band', 'description', 'date_posted', 'download_midi_tabs', 'youtube_link', 'download_guitar_m4v' ];
console.log('inside createColumns: ' + this._columns);
}
getData(data){
console.log('inside getData');
console.log(data);
}
render() {
return (
<div>
{ JSON.stringify(this.data, null, 2) }
</div>
);
}
}
export default Songs;
Here is a sample of two rows from the JSON:
[ { "id": "1", "title": "2 minutes 2 midnight", "original_band": "Iron Maiden", "description": "", "date_posted": "2012-02-06 17:14:56", "download_midi_tabs": "http://kronusproductions.com/songs_angular/assets/downloads/2_minutes_to_midnight.zip", "youtube_link": "http://youtu.be/VAm3-vyfJwI", "download_guitar_m4v": "http://kronusproductions.com/uploads/serious/2-minutes-to-midnight-vst.mp3", "download_enabled": "1" }, { "id": "2", "title": "909", "original_band": "The Beatles", "description": "Even though this song would be recorded at the end of the Beatles time together, it was one of the first songs that Lennon and McCartney wrote together. I have added horns in the place of the guitar licks that Lennon recorded.", "date_posted": "2012-02-06 17:14:56", "download_midi_tabs": "http://kronusproductions.com/songs_angular/assets/downloads/909.zip", "youtube_link": "https://youtu.be/DxuEYgbUtqE", "download_guitar_m4v": "http://kronusproductions.com/uploads/serious/909-vst.mp3", "download_enabled": "1" }
Upvotes: 0
Views: 2420
Reputation: 112787
You have to use setState
to set your component state, and this state will reside in state
of the component.
Example
class Songs extends React.Component {
state = {
data: []
};
componentDidMount() {
fetch("http://localhost:3000/data.json")
.then(response => response.json())
.then(responseJson => {
this.setState({ data: responseJson });
})
.catch(error => {
console.error(error);
});
}
render() {
return (
<div>
{this.state.data.map(obj => {
return (
<div>
<h2>{obj.title}</h2>
<h3>{obj.original_band}</h3>
<p>{obj.description}</p>
</div>
);
})}
</div>
);
}
}
Upvotes: 1
Reputation: 1386
Your code has a bunch of issues, but I think the biggest one is that you never make any call to setState
. Updating of state
or props
is how React knows to trigger a render.
Upvotes: 0