Skip to content

Instantly share code, notes, and snippets.

@Polyrhythm
Created February 24, 2015 00:55
Show Gist options
  • Save Polyrhythm/edf19aead4c83e5d1e7f to your computer and use it in GitHub Desktop.
Save Polyrhythm/edf19aead4c83e5d1e7f to your computer and use it in GitHub Desktop.
var CLIENT_ID = 'nope';
class Matcher extends React.Component {
constructor(props: { imageCount: number }) {
super(props);
this.state = { images: [] };
this.fetchRandomImages(this.props.imageCount);
}
fetchRandomImages(imageCount) {
var req = new XMLHttpRequest();
function success() {
var images = JSON.parse(req.response)
.data
.slice(0, this.props.imageCount);
this.setState({
images: images
});
}
function error(e) {
console.log('error', console.log(req.responseText));
}
req.addEventListener('load', success.bind(this), false);
req.addEventListener('error', error, false);
req.open('get', 'https://api.imgur.com/3/gallery/random/random/0', true);
req.setRequestHeader('Authorization', `Client-ID ${CLIENT_ID}`);
req.send();
}
render() {
var images;
if (!this.state.images.length) {
return <div>we do not have images!</div>;
}
images = this.state.images.map(function(image) {
return <li><Image src={image.link} title={image.title} /></li>;
});
return <ul>{images}</ul>;
}
}
Matcher.defaultProps = { imageCount: 6 };
React.render(<Matcher />, document.querySelector('#app'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment