NextWeb.js

NextWeb.js

  • Docs
  • GitHub

›App

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

Context

Most data in our app should be placed in local state if possible. However, some data (e.g. locale preference, UI theme) are required by many components within an app.

If we decide to keep those data in local state, we have to place them at the root component of our app so we can pass those data through the component tree but this has to be done manually at every component level.

The better way is using context which provides a way to pass data through the component tree without having to pass props down manually.

Built-In Contexts

Fortunately, NextWeb.js comes with some useful built-in contexts that will make your life easier and you can access them using react hooks.

useUA()

There are some useful information NextWeb.js extracted from user agent:

NameDescription
isMobileIs the device a mobile ?
isTabletIs the device a tablet ?
isDesktopIs the device a desktop ?
isIEIs the browser of the device an Internet Explorer ?

For example:

import React, { useContext } from 'react'
import { useUA } from '@lib/userAgent'

function MyComponent() {
  const { device: { isMobile } } = useUA()

  return (
    <div>
      { isMobile ? <MobileUI /> : <DesktopUI /> }
    </div>
  )
}

useMember()

You can access authentication data of a user easily using this react hook:

import React, { useContext } from 'react'
import { useMember } from '@lib/auth'

function MyComponent() {
  const { isAuthenticated } = useMember()

  return (
    <div>
      {isAuthenticated ? <DashboardPage /> : <LoginPage /> }
    </div>
  )
}

See more detail about authentication data in Authentication section

← RoutingStore →
  • Built-In Contexts
    • useUA()
    • useMember()