import 'whatwg-fetch'; import React from 'react'; import PropTypes from 'prop-types'; import Message from './Message'; import Spinner from './Spinner'; export default class Discover extends React.Component { constructor(props) { super(props); this.state = { msgs: [], loading: false, search: this.props.location.search }; this.loadMessages = this.loadMessages.bind(this); } componentDidMount() { this.loadMessages(); } componentWillReceiveProps(nextProps) { if (this.props.location.search != nextProps.location.search) { this.loadMessages(nextProps.location.search) } } loadMessages(filter = '') { this.setState({ msgs: []}) const url = 'https://api.juick.com/messages' + filter; fetch(url) .then(response => { return response.json() }) .then(data => this.setState({ msgs: data }) ).catch(ex => { console.log(ex); }); } render() { var nodes = this.state.msgs.map(msg => { return (); }); return this.state.msgs.length > 0 ? (
{nodes}
) : ; } } Discover.propTypes = { msgs: PropTypes.array };