# rn-shake

> React Native useShake hook that helps detecting shake gesture with phone accelerometer.

Latest version **1.0.3** (published 2021-10-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install rn-shake
pnpm add rn-shake
yarn add rn-shake
bun add rn-shake
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2021-10-11 |
| First published | 2021-10-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 15.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Dmytro Kurovskyi |
| Maintainers | kurovskyi |
| Keywords | react, react-native, shake, sensor, accelerometer, gesture |

## Links

- npm: https://www.npmjs.com/package/rn-shake
- Repository: https://github.com/kurovskyi/rn-shake
- Homepage: https://github.com/kurovskyi/rn-shake#readme
- Issues: https://github.com/kurovskyi/rn-shake/issues
- npm.io page: https://npm.io/package/rn-shake

## Dependencies (1)

- [rxjs](https://npm.io/package/rxjs.md) ^7.4.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.3 (latest) — 2021-10-11
- 1.0.2 — 2021-10-11
- 1.0.1 — 2021-10-08

## README

# rn-shake

<!-- ![preview-cover](./docs/images/preview.png) -->

> 📱 React Native useShake hook that helps detecting shake gesture with phone accelerometer.

## 📦 Installation

```sh
yarn add rn-shake react-native-sensors
```

## 🚀 Usage

- The first argument of the hook is the callback function.
- Second argument is the configuration object - All fields optional.
- It is strongly recommended that you adjust the `threshold` value with experiments to match your needs.

```ts
import {useShake} from 'rn-shake';

useShake(
  () => {
    console.log('🐍 Shook Shook');
  },
  {
    threshold: 3,
    sensorUpdateInterval: 200,
    minimalTimeBetweenTriggers: 1000,
    isTriggerAfterShakes: true,
    resetDependencies: [],
  }
);
```

<!-- **Please, don't use this module for production yet! (See issues)** -->

<!-- [![npm version](https://img.shields.io/npm/v/use-shake-callback.svg?style=flat-square)](https://www.npmjs.org/package/rn-shake)
[![weekly downloads](https://img.shields.io/npm/dw/use-shake-callback.svg?style=flat-square)](https://www.npmjs.org/package/rn-shake)
[![npm bundle size](https://img.shields.io/bundlephobia/minzip/use-shake-callback.svg?style=flat-square)](https://www.npmjs.org/package/rn-shake)
[![license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](./LICENSE)

[![NPM](https://nodei.co/npm/use-shake-callback.png)](https://nodei.co/npm/use-shake-callback/)

## 📦 Installation

- Please note that this package uses `rxjs` and `react-native-sensors` as dependencies and have `react` and `react-native` as peer dependencies.

```bash
npm add use-shake-callback
# Or with yarn
yarn add use-shake-callback
```

## 🚀 Usage

- The first argument of the hook is the callback function - It's only argument is `speed`, which is the speed of your shake at the time the callback is called.
- Second argument is the configuration object - All fields optional.
- It is strongly recommended that you adjust the `threshold` value with experiments to match your needs.

```ts
import useShakeCallback from 'use-shake-callback';

useShakeCallback(
  () => {
    console.log('🐍 Shook Shook');
  },
  {
    updateInterval: 100,
    threshold: 5,
    onError: (error) => console.log(error),
  }
);
```

- This hooks is **optimized to unsubscribe the sensor event listener** when the app status change to `inactive`(= when the app goes to background). The `appState`, which is required to handle such process, is exported so that you can use throughout your application.

```ts
const { appState } = useShakeCallback(
  () => console.log('🐍 Shook Shook');
  {
    onError: (error) => console.log(error),
    onUnsubscribe: () =>
      console.log('[*] Unsubscribing accelerometer sensor'),
  },
);

console.log(appState) // 'active' or 'inactive'
```

## 👻 Typings

- All typings are exported. Yay! 🎉

```ts
import {ShakeCallback, ShakeConfig} from 'use-shake-callback';
```

### type ShakeCallback

- Type of the callback, which is called when the current device speed(calculated from acceleration) is greater then the provided `threshold`.
- Return type is `void`.

### Parameters

| Name    | Type     | Description                |
| ------- | -------- | -------------------------- |
| `speed` | `number` | Speed of the current shake |

### interface ShakeConfig

- Configuration object of the hook.

### Fields

- All fields are optional and have defaults.

| Name             | Type                                  | Default value | Description                                                                                                                 |
| ---------------- | ------------------------------------- | ------------- | --------------------------------------------------------------------------------------------------------------------------- |
| `updateInterval` | `number`                              | `100`         | Defines the update interval of the accelerometer sensor                                                                     |
| `threshold`      | `number`                              | `5`           | Threshold for detecting shake events; If the current speed is greater than the provided value, the callback will be called. |
| `onError`        | `(error: any) => void` or `undefined` | `undefined`   | Called when error occurred in subscription for accelerometer                                                                |
| `onUnsubscribe`  | `() => void` or `undefined`           | `undefined`   | Called when unsubscribed from the accelerometer sensor                                                                      | -->

---
_Source: https://npm.io/package/rn-shake · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
