Last active
May 28, 2019 23:22
-
-
Save sebas5384/f8a0ce26705eec057df31c18a4043da5 to your computer and use it in GitHub Desktop.
Recompose HoCs for Next.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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