use-ssr

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

Github星跟踪图

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()

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

主要指标

概览
名称与所有者iamthesiz/use-ssr
主编程语言TypeScript
编程语言TypeScript (语言数: 2)
平台
许可证MIT License
所有者活动
创建于2019-05-22 00:00:33
推送于2023-03-14 23:21:29
最后一次提交2023-03-14 19:21:29
发布数0
用户参与
星数262
关注者数4
派生数6
提交数145
已启用问题?
问题数7
打开的问题数1
拉请求数28
打开的拉请求数7
关闭的拉请求数16
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?