Skip to content

Instantly share code, notes, and snippets.

@gnppro
Created October 9, 2018 04:39
Show Gist options
  • Select an option

  • Save gnppro/a8d8f68c037aec5e63cc6e8d5d471557 to your computer and use it in GitHub Desktop.

Select an option

Save gnppro/a8d8f68c037aec5e63cc6e8d5d471557 to your computer and use it in GitHub Desktop.
import Layout from '../components/MyLayout.js'
import Link from 'next/link'
// import fetch from 'isomorphic-unfetch'
import axios from 'axios'
const Index = (props) => (
<Layout>
<h1>Batman TV Shows</h1>
<ul>
{props.data.map(({show}) => (
<li key={show.id}>
<Link as={`/p/${show.id}`} href={`/post?id=${show.id}`}>
<a>{show.name}</a>
</Link>
</li>
))}
</ul>
</Layout>
)
Index.getInitialProps =
async function() {
// fetch('https://api.tvmaze.com/search/shows?q=batman')
// const data = await res.json()
const res = await axios.get('https://api.tvmaze.com/search/shows?q=batman')
const data = await res.data
console.log(`Show data fetched. Count: ${data.length}`)
return {
data: data
}
}
export default Index
@gnppro
Copy link
Author

gnppro commented Oct 11, 2018

export default class extends React.Component {
static async getInitialProps({ query }) {
let idItem = query.id
let req = await fetch(`http://localhost:3004/api/v1/items/${idItem}`)
//data
let { body: { channel } } = await req.json()
//return { item: item }
return { channel }
}

render() {
const { channel } = this.props

return (
<h1>{ channel.title }</h1>
)
}
}

@gnppro
Copy link
Author

gnppro commented Oct 11, 2018

Varias Request de url API distintas:

let[req1, req2] = awaitPromise.all([
	fetch(url_1),
	fetch(url_2)
])

@yalopov
Copy link

yalopov commented Apr 11, 2019

¿Que diferencia hay entre isomorphic-unfetch y axios al usarlo en next.js?

@giri-jeedigunta
Copy link

Hello ! Do you have an example with axios interceptors?

Copy link

ghost commented Oct 11, 2019

The difference between fetch and axios, just axios gives you more functionality when ordering, but the core of the operation is the same (make requests). At least I see it that way

@Brayan-724
Copy link

Brayan-724 commented Feb 15, 2022

Highlight and format
import Layout from '../components/MyLayout.js'
import Link from 'next/link'
// import fetch from 'isomorphic-unfetch'
import axios from 'axios'

const Index = (props) => (
  <Layout>
    <h1>Batman TV Shows</h1>
    <ul>
      {props.data.map(({show}) => (
        <li key={show.id}>
          <Link as={`/p/${show.id}`} href={`/post?id=${show.id}`}>
            <a>{show.name}</a>
          </Link>
        </li>
      ))}
    </ul>
  </Layout>
)

Index.getInitialProps = 
  
  async function() {
    // fetch('https://api.tvmaze.com/search/shows?q=batman')
    // const data = await res.json()
  const res = await axios.get('https://api.tvmaze.com/search/shows?q=batman')
  const data = await res.data

  console.log(`Show data fetched. Count: ${data.length}`)

  return {
    data: data
  }
}

export default Index

Original post

export default class extends React.Component {
  static async getInitialProps({ query }) {
    let idItem = query.id
    let req = await fetch(`http://localhost:3004/api/v1/items/${idItem}`)
    //data
    let { body: { channel } } = await req.json()
    //return { item: item }
    return { channel }
  }

  render() {
    const { channel } = this.props

    return (
      <h1>{ channel.title }</h1>
    )
  }
}

^^ Original Comment ^^

let [ res1, res2 ] = await Promise.all([
	fetch(url_1),
	fetch(url_2)
]);

^^ Original Comment ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment