NextWeb.js

NextWeb.js

  • Docs
  • GitHub

›Data Fetching

Getting Started

  • Getting Started
  • Folder Structure
  • Create a New Page
  • Navigate Between Pages

App

  • Environment Variables
  • Module Alias
  • Routing
  • Context
  • Store
  • Authentication

Page

  • Layout
  • Meta
  • Breadcrumb
  • Stats
  • Restricted Pages

Data Fetching

  • Repositories & Services
  • Server-Side Fetching
  • Client-Side Fetching
  • Error Handling
  • Custom Error

Styling

  • Local Styles
  • Global Styles
  • Grids
  • Style Helpers
  • Fonts
  • Icons

Deployment

  • Deployment

Server-Side Fetching

When you want to fetch some data, you have to communicate with API via services. For server-side, just call the service inside getInitialProps() of a page-level component.

Page Component

import * as ArticleService from '@features/article/services'

function ArticleDetailPage({ data }) {
  return (
    <div>
      <ArticleDetail data={data} />
    </div>
  )
}

ArticleDetailPage.getInitialProps = async ({ query }) => {
  const data = await ArticleService.getArticleById(query.id)

  return { data }
}

UI Component

function ArticleDetail({ data }) {
  return (
    <article>
      <h1>{data.title}</h1>
      <div dangerouslySetInnerHTML={{ __html: data.body }} />
    </article>
  )
}
← Repositories & ServicesClient-Side Fetching →
  • Page Component
  • UI Component