• Stars
    star
    261
  • Rank 156,024 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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

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

More Repositories

1

react-useportal

🌀 React hook for Portals
TypeScript
890
star
2

fasthacks

⌘ Speed hacks to increase productivity
JavaScript
170
star
3

Interviews

🤓 Data structures in JS & interview questions/algorithms
JavaScript
138
star
4

use-react-modal

🖼 React hook for Modals
TypeScript
80
star
5

alfred-workflows

A list of my favorite workflows for developers.
PHP
72
star
6

the-perfect-portfolio

This is my portfolio! :)
PHP
31
star
7

urs

useRefState, for getting correct values even when in callback functions, and blocking state updates on unmounted components
TypeScript
16
star
8

use-react-storage

🕋 React hook for local storage on the server, browser, and react native
TypeScript
14
star
9

portfolio

Built with React, Relay, GraphQL, and all babel-node's ES2016 features!
JavaScript
13
star
10

email-autocomplete-input

⚡️ Email autocomplete input
JavaScript
11
star
11

readme-template

🔥The most awesome README template out there
10
star
12

react-css-to-js

This is a compiler that translates CSS into JS. (Currently Not Production Quality)
JavaScript
6
star
13

use-cursor

🐭 React hook for customizing the mouse
TypeScript
5
star
14

google-students

This is a template I built for different Google programs specialists to quickly and easily get a website up and running for their event. It's still in the workings so that it's even easier for people to build on, but it gets the job done in the time being.
CSS
5
star
15

mine-sweeper

💣 Minesweeper
JavaScript
4
star
16

MuniModel

Java
1
star
17

vocaldash

PHP
1
star
18

alexcory.com

❄️My portfolio website (WIP)
JavaScript
1
star
19

hacklete

The portfolio for all software engineers.
JavaScript
1
star
20

techtalksfsu

This is the website I created for Google Helpouts and the event at SFSU.
PHP
1
star
21

date-input

A simple date input for React.js
JavaScript
1
star
22

mvc-website

This is a website I built that uses a home made MVC framework. I built it from the ground up. The only thing I didn't build was the ADODB library I used to add and get posts to the database. The database used was MySQL (PHP MyAdmin).
PHP
1
star