-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: handle undefined mediaDevices #830
fix: handle undefined mediaDevices #830
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding this fix. One thing I'm concerned about is that change regarding error swallowing.
src/util.ts
Outdated
@@ -1,5 1,5 @@ | |||
export const isClient = typeof window === 'object'; | |||
|
|||
export const on = (obj: any, ...args: any[]) => obj.addEventListener(...args); | |||
export const on = (obj: any, ...args: any[]) => obj && obj.addEventListener(...args); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if these functions should be touched at all. With your changes in some cases handler bind will silently fail and development or debug will be much harder. Better to have error in that case.
You should leave only changes in src/useMediaDevices.ts
, update the PR and ill merge it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you forbid to make changes in your fork repo it should be like so:
useMediaDevices
:
import { useEffect, useState } from 'react';
import { noop, off, on } from './util';
const useMediaDevices = () => {
const [state, setState] = useState({});
useEffect(() => {
if (!navigator.mediaDevices) return;
let mounted = true;
const onChange = () => {
navigator.mediaDevices
.enumerateDevices()
.then(devices => {
mounted &&
setState({
devices: devices.map(({ deviceId, groupId, kind, label }) => ({ deviceId, groupId, kind, label })),
});
})
.catch(noop);
};
on(navigator.mediaDevices, 'devicechange', onChange);
onChange();
return () => {
mounted = false;
off(navigator.mediaDevices, 'devicechange', onChange);
};
}, []);
return state;
};
export default useMediaDevices;
util
export const isClient = typeof window === 'object';
export const on = (obj: any, ...args: any[]) => obj.addEventListener(...args);
export const off = (obj: any, ...args: any[]) => obj.removeEventListener(...args);
export const noop = () => {};
@streamich maybe make a breaking change and change the returning signature? |
🎉 This PR is included in version 13.22.5 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Description
Fix: useMediaDevices(): TypeError: Cannot read property 'addEventListener' of undefined
Type of change
Checklist
yarn test
)yarn lint
). Fix it withyarn lint:fix
in case of failure.yarn lint:types
).