1.0.24 ā€¢ Published 3 years ago

use-ssr v1.0.24

Weekly downloads
31,038
License
MIT
Repository
github
Last release
3 years ago

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. šŸ”„

npm.io

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

  • tests for array destructuring
  • set up code climate test coverage
  • add typescript array return types
  • optimize badges see awesome badge list
    • add code climate test coverage badge
  • codesandbox examples
  • continuous integration
  • greenkeeper