1.0.1 • Published 10 years ago

window-or-global v1.0.1

Weekly downloads
120,538
License
MIT
Repository
github
Last release
10 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@everymundo/em-cmp-lib-pricescarbon-addons-boomerang-react@ceed/components-react@smallstack/data@infinitebrahmanuniverse/nolb-windmaycur-business@everything-registry/sub-chunk-3148with-globalxesto-wavexhipsubmesh-treadmilltestfabricthigvue-di-kitipfs-redux-bundleibm-dotcom-library-reactguld-configguld-config-cliguld-fsguld-keysguld-ledgerguld-ledger-grantguld-userguld-user-cliguld-git-confighellostevenmath3d-component@wanmi/x-site-core@wanmi/x-site-ui-server@wfp/react@wfp/ui@worldsibu/convector-core-controller@worldsibu/convector-core-model@worldsibu/convector-core-storage@xlera/convector-core-controller@xlera/convector-core-model@xlera/xplor-core-controller@xlera/xplor-core-model@xlera/xplor-core-storage@xlera/convector-core-storage@un/react@thetrg/tamed@unitednations/react@telnyx/video@telnyx/video-react-native@thecore/fullpage-react@zingle/ui-componentsGitHub-Network-Graphaxios-react@grafite/helpers@hauxir/react-twitch-embed-video@notabug/gun-sear@hieuquang2212/form@idn/identity@ibmdotcom/utilities@ibmdotcom/react@ibmdotcom/services@plasmicapp/hostfullpage-reactfunag@sugar-ai/abcd@sugar-ai/core@sugar-ai/copilot-one-jsem-cmp-lib-prototypeseonin-clienteonin-sharedfixed_form_builderfabrictestsfabric_newnewfabricbuild@pulkit21/aidummy@progressiveui/react@pubcore/redux-browser-history@boomerang-io/gatsby-theme-boomerang@burst/js-utils@carbon/ibmdotcom-patterns-react@carbon/ibmdotcom-vanilla@carbon/ibmdotcom-web-components@carbon/ibmdotcom-utilities@carbon/react@chaingun/sear@bebraw/react-over-scroll@carbon/ibmdotcom-services@carbon/ibmdotcom-reactcan-promisecarbon-react-agavecarbon-ailinkedlaw-reactccr-with-typescarbon-components-react-fork-brcloudinary-mobx-responsive-imagecycle-canvas@april9/stack9-react@april9/stack9-ui@apex-org/convector-core-controller@apex-org/convector-core-model@apex-org/convector-core-storage@apex-pub/convector-core-controller@apex-pub/convector-core-model@apex-pub/convector-core-storage
1.0.1

10 years ago

1.0.0

10 years ago