Skip to content

Instantly share code, notes, and snippets.

@sebas5384
Last active May 28, 2019 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebas5384/f8a0ce26705eec057df31c18a4043da5 to your computer and use it in GitHub Desktop.
Save sebas5384/f8a0ce26705eec057df31c18a4043da5 to your computer and use it in GitHub Desktop.
Recompose HoCs for Next.js
/**
* Check if code is running on production mode.
*/
export const isProduction = () =>
typeof process !== "undefined" && process.env.NODE_ENV === "production"
/**
* Check if code is running on development mode.
*/
export const isDevelopment = !isProduction()
/**
* Check if code is running on the client.
*
* If process is available (Next), check if it has a property "browser".
* Otherwise, check if a window object is available.
*/
export const isClient = () =>
typeof process !== "undefined"
? !!process.browser
: typeof window !== "undefined"
/**
* Check if code is running on the server.
*/
export const isServer = !isClient()
/**
* HoC container to compose only when running on client-side.
*/
export const whenClient = (ifHoC, elseHoC) => branch(isClient, ifHoC, elseHoC)
/**
* HoC container to compose only when running on server-side.
*/
export const whenServer = (ifHoC, elseHoC) => branch(isServer, ifHoC, elseHoC)
/**
* HoC container to compose only when running on development mode.
*/
export const whenDevelopment = (ifHoC, elseHoC) =>
branch(isDevelopment, ifHoC, elseHoC)
/**
* HoC container to compose only when running on production mode.
*/
export const whenProduction = (ifHoC, elseHoC) =>
branch(isProduction, ifHoC, elseHoC)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment