Skip to content

Instantly share code, notes, and snippets.

@jeffscottward
Last active August 5, 2019 15:50
Show Gist options
  • Save jeffscottward/206a508ec378da3f1ad1b4c44d5fe86b to your computer and use it in GitHub Desktop.
Save jeffscottward/206a508ec378da3f1ad1b4c44d5fe86b to your computer and use it in GitHub Desktop.
Next.js Data querying example
import React, { Component } from 'react'
const Index = () => (
<div className="IndexPage">
<label>{this.props.APIdata.key}</label>
<span>{this.props.APIdata.value}</span>
<style jsx>{`
.IndexPage { color: blue; }
`}</style>
</div>
)
Index.getInitialProps = () => {
// await response of fetch call
let response = await fetch('https://quantstamp.com/api/v1/JSONDATA')
// only proceed once promise is resolved
let APIdata = await response.json()
// only proceed once second promise is resolved
return { APIdata }
}
export default Index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment