Next JS - fetch API with GET method

0 votes
271 views
added Mar 1, 2019 in Next JS by LC Marshal Captain (25,790 points)
import React from 'react';
import PropTypes from 'prop-types';
import fetch from 'isomorphic-unfetch';
import Layout from '../components/template/Layout';
import MainComponent from '../components/frontpage/MainComponent';

const Api = 'https://yourapisoruce.com/api/true';// production api
const URL = Api + DEFAULT_QUERY;
const Index = ({ JSONdata }) => (
  <Layout classProp="frontpage">
    <FrontMainComponent data={JSONdata} />
    { /* <PartnersComponent /> */ }
  </Layout>
);

Index.getInitialProps = async () => {
  const res = await fetch(URL);
  const data = await res.json();
  // console.log(data.editorsPick);
  return { JSONdata: data };
};

Index.propTypes = {
  // eslint-disable-next-line react/forbid-prop-types
  JSONdata: PropTypes.object.isRequired,
};
export default Index;

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...