# react-hook-recorder

> React based hook to use native MediaRecorder API

Latest version **0.0.9** (published 2021-10-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-hook-recorder
pnpm add react-hook-recorder
yarn add react-hook-recorder
bun add react-hook-recorder
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.9 |
| Published | 2021-10-02 |
| First published | 2021-04-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 18.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | HaFLiNGeR |
| Maintainers | haflinger |
| Keywords | react, hooks, recorder, media, webcam, video, audio |

## Links

- npm: https://www.npmjs.com/package/react-hook-recorder
- Repository: https://github.com/haflinger/react-hook-recorder
- npm.io page: https://npm.io/package/react-hook-recorder

## Dependencies (3)

- [react](https://npm.io/package/react.md) ^17.0.2
- [react-dom](https://npm.io/package/react-dom.md) ^17.0.2
- [@types/dom-mediacapture-record](https://npm.io/package/@types/dom-mediacapture-record.md) ^1.0.7

## 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

- 0.0.9 (latest) — 2021-10-02
- 0.0.8 — 2021-04-20
- 0.0.7 — 2021-04-20
- 0.0.6 — 2021-04-20
- 0.0.5 — 2021-04-19
- 0.0.4 — 2021-04-19
- 0.0.3 — 2021-04-19
- 0.0.2 — 2021-04-19
- 0.0.1 — 2021-04-19

## README

# react-hook-recorder

A simple react hook using [MediaRecorder API](https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/MediaRecorder)

## Demo

https://codesandbox.io/s/react-hook-recorder-gbz6z

## Example

```javascript
import React, { useCallback, useState } from "react";
import useRecorder from "react-hook-recorder";

function Recorder() {
  const [url, setUrl] = useState("");
  const onStop = useCallback((blob, blobUrl) => {
    setUrl(blobUrl);
  }, []);

  const { startRecording, stopRecording, register, status } = useRecorder();

  return (
    <div>
      <video ref={register} autoPlay muted playsInline />
      {url && (
        <>
          Recorded video&nbsp;:
          <video controls src={url} />
        </>
      )}
      {status !== "init" && (
        <>
          <button onClick={startRecording} disabled={status === "recording"}>
            Start Recording
          </button>
          <button
            onClick={stopRecording(onStop)}
            disabled={status !== "recording"}
          >
            Stop Recording
          </button>
        </>
      )}
      <div>
        <strong>Status :</strong>&nbsp;
        {status}
      </div>
    </div>
  );
}

export default Recorder;
```

## API

### _`useRecorder`_

#### `Args` (mediaStreamConstraints: MediaStreamConstraints, mediaRecorderOptions: MediaRecorderOptions)

| Property               | Required | Type     | Description                                                                                                |
| ---------------------- | -------- | -------- | ---------------------------------------------------------------------------------------------------------- |
| mediaStreamConstraints | false    | `object` | [`MediaStreamConstraints`](https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamConstraints) object |
| mediaRecorderOptions   | false    | `object` | [`MediaRecorder`](https://developer.mozilla.org/en-US/docs/Web/API/MediaRecorder/MediaRecorder) object     |

#### `Returns` (object)

| Property       | Type             | Args                                        | Description                        |
| -------------- | ---------------- | ------------------------------------------- | ---------------------------------- |
| mediaRecorder  | `MediaRecorder`  |                                             | MediaRecorder instance ref         |
| stream         | `MediaStream`    |                                             | MediaStream instance ref           |
| startRecording | `function`       |                                             | function to start recording        |
| stopRecording  | `function`       | `function(blob: Blob, url: string) => void` | function to stop recording         |
| register       | `function`       | `HTMLVideoElement`                          | function to register video element |
| status         | `RecorderStatus` |                                             | return recorder status             |
| error          | `RecorderError`  |                                             | return recorder error              |

## Types

#### _`enum RecorderStatus`_ : _`"idle" | "init" | "recording"`_

#### _`enum RecorderError`_ : _`"stream-init" | "recorder-init"`_

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