Registry / web-framework / react-onclickoutside

react-onclickoutside

JSON →
library6.13.2jsnpmunverified

react-onclickoutside is a Higher Order Component (HOC) for React class components, currently at version 6.13.2. It enables wrapped components to detect and respond to click events that occur anywhere in the document outside their own DOM element, commonly used for dismissing menus, modals, or dropdowns. While it supports React versions 15.5.x through 18.x, the library's documentation explicitly recommends against its use with modern React functional components and hooks, suggesting manual implementation of `useRef` and `useEffect` instead. The project indicates it requires community support for continued maintenance. It relies on the `.classList` property, necessitating a polyfill like `dom4` for compatibility with older browsers like Internet Explorer. Releases are irregular, with the last major refactor to ES6 classes occurring in v6.

npm install react-onclickoutside
INSTALL
IMPORT
SIG · REACT-ONCLICKOUTSI
R
react-onclickoutside
web-frameworkjavascriptv6.13.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

onClickOutside
import onClickOutside from 'react-onclickoutside';
import { onClickOutside } from 'react-onclickoutside'; const onClickOutside = require('react-onclickoutside');
The primary export is a default export, a Higher Order Component. For CommonJS, use `.default` as the library is bundled as an ES6 module.
IGNORE_CLASS_NAME
import { IGNORE_CLASS_NAME } from 'react-onclickoutside';
const IGNORE_CLASS_NAME = 'ignore-react-onclickoutside';
Exported constant for the CSS class name used to mark elements that should be ignored by the outside click detection logic. This constant was exposed as of v6.5.0.

This quickstart demonstrates how to use the `onClickOutside` HOC with an ES6 class component to create a dropdown menu that closes when a user clicks anywhere outside of it. The `handleClickOutside` method is a mandatory implementation for components wrapped by this HOC.

import React, { Component } from 'react'; import onClickOutside from 'react-onclickoutside'; class DropdownMenu extends Component { constructor(props) { super(props); this.state = { isOpen: false }; this.toggleMenu = this.toggleMenu.bind(this); } toggleMenu() { this.setState(prevState => ({ isOpen: !prevState.isOpen })); } // This method is required by react-onclickoutside HOC handleClickOutside = (evt) => { // console.log('Clicked outside!', evt.target); if (this.state.isOpen) { this.setState({ isOpen: false }); } }; render() { return ( <div style={{ position: 'relative', display: 'inline-block' }}> <button onClick={this.toggleMenu}>Toggle Dropdown</button> {this.state.isOpen && ( <div style={{ position: 'absolute', backgroundColor: '#f9f9f9', minWidth: '160px', boxShadow: '0px 8px 16px 0px rgba(0,0,0,0.2)', zIndex: 1, padding: '12px 16px', marginTop: '5px' }} > <a href="#" style={{ display: 'block', textDecoration: 'none', color: 'black' }}>Link 1</a> <a href="#" style={{ display: 'block', textDecoration: 'none', color: 'black' }}>Link 2</a> <a href="#" style={{ display: 'block', textDecoration: 'none', color: 'black' }}>Link 3</a> </div> )} </div> ); } } export default onClickOutside(DropdownMenu);
Debug
Known issues
deprecatedThis HOC is not recommended for use with modern React functional components and hooks. The project documentation explicitly advises against installing it if you are using hooks and suggests implementing the logic manually with `useRef` and `useEffect` instead.
fix
For functional components, implement a custom hook using `useRef` and `useEffect` to manage outside click detection. Several alternative libraries also offer hook-based solutions (e.g., `react-cool-onclickoutside`, `@custom-react-hooks/use-click-outside`).
affects: >=6.0.0
maintenanceThe project is currently in maintenance mode, and the README indicates it 'needs your support to stay maintained'. This suggests a potential for slower development, bug fixes, or eventual abandonment without community funding.
fix
Evaluate the project's long-term viability for new applications. For existing applications, consider contributing to its maintenance or migrating to an actively developed alternative, especially if using functional components and hooks.
affects: >=6.0.0
gotchaThe HOC relies on the `.classList` DOM property. Older browsers, particularly Internet Explorer (pre-Edge) and some older SVG implementations in IE, do not fully support `classList`, which can lead to unexpected behavior or errors.
fix
Include a `classList` polyfill (e.g., `dom4`) in your project if you need to support environments that lack native `classList` support.
affects: >=1.0.0
breakingVersion 6.0.0 refactored the library to use ES6 class syntax, discontinuing the `createClass` and mixin patterns used in older versions (e.g., v4.x, v5.x).
fix
Update existing components from `createClass` or mixin patterns to ES6 class components. Ensure `handleClickOutside` is defined as a class method or passed via configuration.
affects: >=6.0.0
breakingAs of v6.0.0, explicit `passive` settings for touch events are passed, based on the `preventDefault` prop. This can alter the default behavior of touch event handling.
fix
Review how touch events are handled in your application, especially if `preventDefault` is used or if you experience unexpected scrolling or touch interaction behavior. Adjust `preventDefault` prop or event listener options if necessary.
affects: >=6.0.0
Errors
Common errors & fixes
Error: Component must implement a handleClickOutside method or pass a handler in the config object.
The component wrapped by `onClickOutside` does not define a `handleClickOutside` method as a class member, or a custom handler was not provided in the HOC's configuration.
fix
Ensure your wrapped class component includes a `handleClickOutside = (event) => { /* ... */ };` method. Alternatively, pass a `handleClickOutside` function as the second argument to `onClickOutside(MyComponent, { handleClickOutside: myCustomHandler })` for more explicit control, especially in TypeScript.
TypeError: Cannot read property 'classList' of undefined (or null)
An element being clicked, or an element in its ancestry, does not have a `classList` property, often due to an older browser (like IE) or attempting to access it on an SVG element in an unsupported environment.
fix
Install and include a `classList` polyfill (e.g., `dom4`) in your project to ensure compatibility across all target browsers. For SVG elements in older IE, manual DOM manipulation or ensuring `classList` polyfill covers SVG might be necessary.
Module not found: Can't resolve 'react-onclickoutside'
This usually indicates incorrect import syntax, a missing dependency in `package.json`, or a problem with module resolution in your bundler.
fix
Verify that `react-onclickoutside` is installed via `npm install react-onclickoutside`. Ensure the import statement is `import onClickOutside from 'react-onclickoutside';` as it is a default export. For CommonJS, use `const onClickOutside = require('react-onclickoutside').default;`
Trying to use `onClickOutside` with functional components or hooks, but it's not working as expected.
The `react-onclickoutside` library is a HOC designed for React class components and does not directly support React hooks or functional components in the same way modern hook-based solutions do.
fix
For functional components, implement a custom hook using `useRef` and `useEffect` to detect outside clicks. This involves attaching a ref to your component and an event listener to the document, checking if the clicked target is outside the ref's current element.
Upgrade
Version history
6.13.2latest on npm
Audit
Dependencies
reactrequiredPeer dependency for React component functionality.
react-domrequiredPeer dependency for DOM manipulation and event binding.
Agent activity
4 hits · last 30 days
node
4
Resources
react-onclickoutside — npm install react-onclickoutside · libregistry