Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

use-ssr

alex-cory326.4kMIT1.0.24TypeScript support: included

☯️ React hook to determine if you're on the server or browser

ssr, nextjs, server, server side rendering, server side, client, browser, isomorphic, react, react-hook, hooks, react hooks

readme

useSSR

☯️ React hook to determine if you are on the server, browser, or react native

undefined undefined Known Vulnerabilities Known Vulnerabilities

Need to know when you're on the server, in the browser or in react native in your components/hooks? This simple hook makes it easy. 🔥

Features

  • SSR (server side rendering) support
  • TypeScript support
  • Zero dependencies
  • React Native support

Examples

Installation

yarn add use-ssr      or     npm i -S use-ssr

Usage

import useSSR from 'use-ssr'

const App = () => {
  var { isBrowser, isServer, isNative } = useSSR()

  // Want array destructuring? You can do that too!
  var [isBrowser, isServer, isNative] = useSSR()

  /*
   * In your browser's chrome devtools console you should see
   * > IS BROWSER: 👍
   * > IS SERVER: 👎
   *
   * AND, in your terminal where your server is running you should see
   * > IS BROWSER: 👎
   * > IS SERVER: 👍
   */
  console.log('IS BROWSER: ', isBrowser ? '👍' : '👎')
  console.log('IS SERVER: ', isServer ? '👍' : '👎')
  console.log('IS NATIVE: ', isNative ? '👍' : '👎')
  return (
    <>
      Is in browser? {isBrowser ? '👍' : '👎'}
      <br />
      Is on server? {isServer ? '👍' : '👎'}
      <br />
      Is react native? {isNative ? '👍' : '👎'}
    </>
  )
}

Options

const {
  isBrowser,
  isServer,
  isNative,
  device, // 'server', 'browser', or 'native'
  canUseWorkers,
  canUseEventListeners,
  canUseViewport,
} = useSSR()
// OR
const [
  isBrowser,
  isServer,
  isNative,
  device, // 'server', 'browser', or 'native'
  canUseWorkers,
  canUseEventListeners,
  canUseViewport,
] = useSSR()

Who Uses useSSR?

Todos

  • <input disabled="" type="checkbox"> tests for array destructuring
  • <input disabled="" type="checkbox"> set up code climate test coverage
  • <input disabled="" type="checkbox"> add typescript array return types
  • <input disabled="" type="checkbox"> optimize badges see awesome badge list
    • <input disabled="" type="checkbox"> add code climate test coverage badge
  • <input checked="" disabled="" type="checkbox"> codesandbox examples
  • <input checked="" disabled="" type="checkbox"> continuous integration
  • <input checked="" disabled="" type="checkbox"> greenkeeper