Skip to content

Instantly share code, notes, and snippets.

@CharlesMangwa
Last active March 15, 2018 19:41
Show Gist options
  • Save CharlesMangwa/12f56344649394557e1bdb13c8b199e2 to your computer and use it in GitHub Desktop.
Save CharlesMangwa/12f56344649394557e1bdb13c8b199e2 to your computer and use it in GitHub Desktop.
React Data Fetching - `requestToApi` use case
import React, { Component } from 'react'
import { requestToApi } from 'react-data-fetching'
// Just here for better understanding
import { ChatFeed } from './components'
export default class Chat extends Component {
state = { messages: null }
async componentDidMount() {
const apiResponse = await requestToApi({
path: 'https://api.nyan.com/openChat',
method: 'GET',
params: {
start: 0,
limit: 100
}
})
if (apiResponse.isOK)
this.setState(() => ({ messages: apiResponse.result }))
}
render() {
<ChatFeed data={this.state.messages} />
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment