Installation
npm install use-media-stream# oryarn add use-media-streampnpm add use-media-streambun add use-media-streamThat’s it. The package has zero runtime dependencies — react >= 16 as a peer dependency is
the only thing it expects, and you already have that.
What you get
Section titled “What you get”The package ships both module formats with types for each, so it works whether your project is ESM, CommonJS, or bundled:
import useMediaStream from 'use-media-stream'; // ESMconst useMediaStream = require('use-media-stream'); // CommonJSIt is published with provenance, so you can verify the tarball on npm was built by the repository’s release workflow:
npm audit signaturesRequirements
Section titled “Requirements”| React | >= 16 (peer dependency) |
| Node | >= 18, only if you render server-side |
| Browser | Anything with getUserMedia — every current browser |
getUserMedia requires a secure context, so the camera works on https:// and on
localhost, but not on a plain http:// origin.
Upgrading from v1
Section titled “Upgrading from v1”v2 is a correctness release. The API is unchanged apart from additions, but the lifecycle fixes change behaviour by definition — two of them turn the camera off in cases where v1 left it on.
See Migrating from v1 for the full list. The short version:
stop()releases any stream the hook holds, not just one started bystart()- Unmounting releases the stream
errorisError | null, wasunknown- Request states are a union, were
string
- Quick start — a working camera in about fifteen lines
- Live demo — every function wired to a real camera