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

Client-Side Fetching

Just like server-side fetching, you have to use services. But for client-side, you do not have to touch getInitialProps() of a page-level component.

Fetch Component

NextWeb.js comes with a <Fetch /> component which makes client-side data fetching super easy. It lets you focus on the data instead of handling loading and error status.

Here are available props of the <Fetch /> component:

NameDescription
serviceA service function that returns a promise
onErrorA callback function to handle an error
preloaderReact Component / Element to render while loading

Page Component

import { Fetch } from '@lib/api'

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

function ArticleDetailPage() {
  return (
    <div>
      <Fetch service={() => ArticleService.getArticles({ limit: 10 }))}>
        {({ data }) => <ArticleLatest data={data} />}
      </Fetch>
    </div>
  )
}

UI Component

function ArticleLatest({ data }) {
  return (
    <section>
      <h2>Latest Articles</h2>
      <div>
        {data.map(article => (
          <article key={article.id}>
            <h3>
              <Link route="article-detail" params={{ id: data.id }}>
                <a>{data.title}</a>
              </Link>
            </h3>
            <div dangerouslySetInnerHTML={{ __html: data.excerpt }} />
          </article>
        ))}
      </div>
    </section>
  )
}
← Server-Side FetchingError Handling →
  • Fetch Component
  • Page Component
  • UI Component