1.0.1 • Published 9 years ago

window-or-global v1.0.1

Weekly downloads
120,538
License
MIT
Repository
github
Last release
9 years ago

window-or-global

Use this module to get the global object both on server and client side. No more window is not defined errors just peace and flowers.

Advantages:

  • You can easily mock it by mockery or proxyquire in your tests
  • It's really useful in case of universal (or isomorphic) code, for example, when you'd like to render a React component both on client and server side

Install

$ npm i window-or-global

Usage

import root from 'window-or-global'

React example

components/my-component.jsx:

import React, { Component } from 'react'
// in node, you'll get the global object instead of crashing by an error
import root from 'window-or-global'

class MyComponent extends Component {

  // this method is only invoked in the browser environment
  componentDidMount() {
    root.addEventListener(/*...*/)
  }

  componentWillUnmount() {
    root.addEventListener(/*...*/)
  }

  render() {}

}

// Voilà. Enjoy your universal react component! ;)
// No more 'window is not defined' errors when you render your component
// on server side.

Unit-testing example

You can find a complete test example here. The test passes of course, just type

$ npm test

in your terminal.

License

MIT © Purpose Industries

behtarino@carbon/ibmdotcom-react@carbon/ibmdotcom-services@carbon/ibmdotcom-utilities@carbon/ibmdotcom-web-components@everymundo/em-cmp-lib-pricescarbon-addons-boomerang-react@ceed/components-react@smallstack/data@infinitebrahmanuniverse/nolb-windmaycur-business@everything-registry/sub-chunk-3148github-profile-card-componentguld-keysguld-ledgerguld-ledger-grantguld-userguld-user-cliguld-fsguld-git-configguld-configguld-config-clihellostevenipfs-redux-bundleibm-dotcom-library-reactmath3d-componentmerchant-dashboardnestjs-testnestjs-clientng-carbon-components-reactnb-vue-utilsnestjs-corenosmoke@un/react@unitednations/react@plasmicapp/host@thetrg/tamed@sugar-ai/abcd@sugar-ai/copilot-one-js@telnyx/video@telnyx/video-react-native@thecore/fullpage-react@rocketsoftware/carbon-components-reactwith-globalvue-di-kitxesto-wavexhipyodata-actionstestfabricsubmesh-treadmillsundukthigqry-businessreact-canvas-statsreact-mailto.jsreact-haptic-buttonreact-imgproredux-hydration-examplereact-twitch-embed-videosofologyfabricserverrequests-webclientstewie-editor@amazingandyyy/next-deck@evkj/x-site-core@evkj/x-site-ui-server@fang-w-shen/storybook@hieuquang2212/form@hauxir/react-twitch-embed-video@loopmode/events@mrpt/mrpt-web@mosanic/host@mskcc/carbon-react@ibmdotcom/react@ibmdotcom/services@ibmdotcom/utilities@jimpick/ipfs-provider@notabug/gun-sear@department-of-veterans-affairs/jean-pants@devniel/carbon-components-react@chaingun/sear@burst/js-utils@boomerang-io/gatsby-theme-boomerang@convector/convector-core-controller@convector/convector-core-model@convector/convector-core-storage@apex-org/convector-core-controller@apex-org/convector-core-model@apex-org/convector-core-storage@apex-pub/convector-core-model@apex-pub/convector-core-storage@apex-pub/convector-core-controller@carbon/ibmdotcom-vanilla@carbon/ibmdotcom-patterns-react@carbon/react@d-cat/mocks-google-analytics-jest@pulkit21/aidummy@pubcore/redux-browser-history@bebraw/react-over-scroll@april9/stack9-react@april9/stack9-ui
1.0.1

9 years ago

1.0.0

9 years ago