An open API service indexing awesome lists of open source software.

https://github.com/omtanke/react-use-event-outside

It allows you to perform your operations in locations other than the place marked with ref.
https://github.com/omtanke/react-use-event-outside

react reacthooks ref

Last synced: 9 months ago
JSON representation

It allows you to perform your operations in locations other than the place marked with ref.

Awesome Lists containing this project

README

          

# react-use-event-outside

## Installation

Install with [Yarn](https://yarnpkg.com/):

```sh
yarn add @omtanke/react-use-event-outside
```

Or with [NPM](https://www.npmjs.com/):

```sh
npm i @omtanke/react-use-event-outside
```

Import into your component like so:

```javascript
import useEventOutside from '@omtanke/react-use-event-outside';
```

## Usage

```javascript
useEventOutside(REF, EVENT_NAME, FUNCTION);
```
**REF**: Outside which element you want to trigger.

**EVENT_NAME**: You want to listen to the event (Ex: 'mousedown', 'touchstart', etc.).

**FUNCTION**: The function you want to run when triggered.

### Example

[CodeSandbox](https://codesandbox.io/s/useeventoutside-6gfby?file=/src/App.js)

or

```javascript
const App = () => {
const ref = useRef(null);
const [isOpen, setIsOpen] = useState(false);

const closeMenu = useCallback(() => {
setIsOpen(false);
}, []);

useEventOutside(ref, 'mousedown', closeMenu);

return (

setIsOpen(!isOpen)}>Toggle
{isOpen &&


  • Item

  • Item


}

);
};
```