I'm learning React from self learning and need help below for a task. Please apologise for my poor code, still learning.
The main focus of this is trying to use this.setState to push the contents I have gathered from the JSON file to my state as shown below.
(please notice that the list state was a test to see if I was using this.setState correctly) (comments are things I have tried but found they have not given me the result I was looking for)
import React, { Component } from 'react';
export default class App extends Component { constructor(props) { super(props); this.state = { items: [], titles: [], list: [1, 2, 3], } //this.getFlickrApi = this.getFlickrApi.bind(this); }
I'm currently trying to create a simple app that displays pictures from Flickr. Currently using the API URL: https://api.flickr.com/services/feeds/photos_public.gne?format=json&&nojsoncallback=1
I'm connecting to this with the code below
componentDidMount() { var request = new XMLHttpRequest();
request.open('GET',
'https://api.flickr.com/services/feeds/photos_public.gne?
format=json&&nojsoncallback=1', true);
var dataOutput = request.onload = function() {
if (request.status >= 200 && request.status < 400) {
console.log('Before JSON.parse');
var data = JSON.parse(request.response);
console.log(data);
//this.setState({ items: data.items })
console.log('After JSON.parse');
} else {
console.log('error');
}
dataOutput = data;
console.log(dataOutput.items[1].title)
//dataOutput.items.forEach()
//this.setState({items: dataOutput.items});
}
request.send();
}
And I'm trying to use this to display the 'titles' of each 'item' within the render function.
render() {
// Use below for a function
// data.items.forEach(item => {
// console.log(item.title)
// //this.setState({titles: data.items.titles})
// //console.log(this.state.titles)
// });
//console.log(this.state);
//console.log(getFlickrApi());
// {this.getFlickrApi()}
return (
<div className="FlickrApiContainer">
))}
{this.state.items.map(item => (<li key={item}>{item.title}</li>))}
{this.renderItems()}
</div>
);
}
}
But it's not displaying within the list tags despite I can console to it.
Any help appreciated.
Kind regards,
Sam